pypars解析用分号而不是逗号的csv文件 [英] pyparsing parsing csv files with semi-colon instead of comma

查看:124
本文介绍了pypars解析用分号而不是逗号的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在欧洲大陆,csv文件用分号分隔,因为数字中包含而不是.所以,我试图写一个与commaSeparatedList相同的semicolonSeparatedList,但带有;.代替,:

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")

但是解析:

Name;Ref;Address 

结果

['Name'] 

代替

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

任何人都可以帮忙吗?

推荐答案

您是否尝试过python中的csv模块?

在那里,您可以轻松指定定界符.

There, you can specify the delimiter easily.

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

在Birei评论后进行我只是从docs python页面获取示例,您可以输入任何想要用作csv阅读器定界符的内容:
''
','
';'
'a'

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'

这篇关于pypars解析用分号而不是逗号的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆