pyparsing parsing csv files with semi-colon instead of comma

♀尐吖头ヾ 提交于 2021-02-19 06:20:11

问题


In mainland europe, the csv files are separated through semicolons because numbers have , in them instead of . So, i am trying to write a semicolonSeparatedList same as commaSeparatedList but with ; instead of ,:

_semicolonsepitem = Combine(OneOrMore(Word(printables, excludeChars=';') +
                             Optional( Word(" \t") +
                                       ~Literal(";") + ~LineEnd() ) ) ).streamline().setName("semicolonItem")
semicolonSeparatedList = delimitedList( Optional( quotedString.copy() | _semicolonsepitem, default="") ).setName("semicolonSeparatedList")

However parsing:

Name;Ref;Address 

results in

['Name'] 

instead of

['Name', 'Ref', 'Address']

Can anyone help?


回答1:


Have you tried the csv module from python?

There, you can specify the delimiter easily.

import csv
with open('eggs.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')

Edit after Birei's comment : I just took example from docs python page, you can input anything you want as a delimiter for csv reader :
' '
','
';'
'a'



来源:https://stackoverflow.com/questions/19514602/pyparsing-parsing-csv-files-with-semi-colon-instead-of-comma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!