Home > database >  Reading number between characters from a csv file python
Reading number between characters from a csv file python

Time:03-16

I have this file with this data:

0,901,48.892795924112306,2.391225227186182,20
1,903,48.83713368945151,2.374340554605615,20
2,904,48.85213620522547,2.301961227213259,30
3,905,48.83966087889425,2.382472269083633,20
4,906,48.876419813641114,2.358630064544601,20

I want to print the number between 2 commas (,) alone on a new line but without the first column and the last column

example of the desired output:

901
48.892795924112306
2.391225227186182
903
48.83713368945151
2.374340554605615

and so on...

CodePudding user response:

Here a sample:

csv = """0,901,48.892795924112306,2.391225227186182,20
1,903,48.83713368945151,2.374340554605615,20
2,904,48.85213620522547,2.301961227213259,30
3,905,48.83966087889425,2.382472269083633,20
4,906,48.876419813641114,2.358630064544601,20"""

for row in csv.split():
    print('\n'.join(row.split(',')[1:-1])) 

You should open the file and read each line (task for you).

row.split(',')[1:-1] slice the row by omitting the 1st and last entries which are those surrounded by the ,

  • Related