gpt4 book ai didi

python - 在python中用引号解析逗号分隔的csv文件

转载 作者:太空狗 更新时间:2023-10-29 20:11:40 26 4
gpt4 key购买 nike

下面我有一个字符串,代表从 csv 文件中提取的一行。每列用逗号分隔,值用“”包裹。在 python 中解析每一列的值的最简单方法是什么?

"Mr","Bob","","Boberton","","President","","","","Blah, Inc. of Iowa","blah blah blah","","Grand Island","Hall County","NE","68801","7228","United States","308-111-1111","","","P.O. BOX 1111","","Grand Island","Hall County","NE","11111","1111","United States","","40.00000","-98.0000","Gasoline service stations","11111","1010101010","","","false","SINGLE_LOCATION","","","","","","","","","No","No","No","Owns","No","No","","","55.125905","","0.052369","","100","","100","0","","","1971","Low Risk","Convenience Stores & Truck Stops","1111111","1111111","111111","1111111"

上面的数据都是一个大字符串,需要拆分成列,这样我才能得到值。我不是在问如何加载 csv 文件。已经弄清楚了那部分。

最佳答案

Python 有一个模块:

http://docs.python.org/library/csv.html

import csv, sys
filename = 'some.csv'
with open(filename, 'rb') as f:
reader = csv.reader(f)
try:
for row in reader:
print row
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))

关于python - 在python中用引号解析逗号分隔的csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com