简体   繁体   中英

Python: How to read a CSV file in which each line is a string?

I am trying to read a CSV file. I need to access the keys and values in each line.

"{id:495981,start:""2020-09-23"",end:""2020-09-23"",something:point({srid:4326, x:10.96791704, y:49.7989944})}"
"{id:49963,start:""2020-09-23"",end:""2020-09-23"",something:point({srid:4326, x:10.96791704, y:49.7989944})}"

As shown above, each line is a string. What I want to do is reading the value of id in each line. Reading the file with "panda.read_csv" return something like this:

  {id:495981   end:""2020-09-23""  start:""2020-09-23""  \
    0      {id:49963   end:""2020-09-23""  start:""2020-09-23""  
           ...
    something:point({srid:4326         x:7.138        y:51.594})}  
    0      something:point({srid:4326   x:10.96791704    y:49.7989944})}
[31264 rows x 6 columns]

Any suggestions??

You could utilize regex here to pull each result out of the string as splitting would include the extra characters I'm assuming you would want to exclude.

import re

data = {}

with open('mycsvfile.csv', 'r') as file:
    for line in file:
        line_id = re.search('(?<=id:)[0-9]*(?=,)', line).group(0)
        line_data = {'start': re.search('(?<=start:"").*(?="",end)', line).group(0),
                     'end': re.search('(?<=end:"").*(?="",something)', line).group(0),
                     'something': re.search('(?<=something:).*(?=}")', line).group(0),
                     }
        data[line_id] = line_data

print(data)

This will result in a dict with all ids as a key with each key containing another dict with all the values in the string.

{'495981': {'start': '2020-09-23', 'end': '2020-09-23', 'something': 'point({srid:4326, x:10.96791704, y:49.7989944})'}, 
'49963': {'start': '2020-09-23', 'end': '2020-09-23', 'something': 'point({srid:4326, x:10.96791704, y:49.7989944})'}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM