Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyparsing parsing csv files with semi-colon instead of comma

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?

like image 355
Dan Bunea Avatar asked Sep 01 '25 09:09

Dan Bunea


1 Answers

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'

like image 200
kv000 Avatar answered Sep 05 '25 12:09

kv000