Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

python - Nice file parsing required

I need to process a file like this

keyword,synonym,bidirectional
5487500j,54875,false
76x76,76 x 76,true
feuille,"papier,ramette",false
7843000j,78430,false

and I need to transform it to a dict :

{'5487500j':'54875', '76x76':'76 x 76','feuille':['papier','ramette'], '7843000j':'78430'}

I don't succeed in any fast and elegant way to deal


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let me first specify what I have understood from your requirement.

  • You input is a csv file, with optionaly quoted fields: ok the csv module can parse it
  • The first field of each record will be used as a key in a dictionary
  • the third field is ignored
  • the second field will be the value in the dictionary. If it does not contain a comma, it will be used as is, else the value will be a splitted list

You should always write down in plain english (or whatever you first language is) a detailed specification of what you want to do before trying to code anything. Because the coding part should only be the translation of the spec.

Here the Python code (for my spec...) could be:

with open(inputfile) as fd:
    rd = csv.reader(fd)  # you files uses the default for quoting and delimiter
    _ = next(rd)         # skip header line
    result = {}
    for row in rd:
        result[row[0]] = row[1].split(',') if ',' in row[1] else row[1]

In fact a comprehension would be more Pythonic than the loop:

    result = {row[0]: row[1].split(',') if ',' in row[1] else row[1]
              for row in rd}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...