问题
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