解析逗号分隔csv文件与引号在python [英] parse comma separated csv file with quotes in python

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

问题描述

下面我有一个字符串表示从csv文件拉出的单个行。每个列由逗号分隔,值包装在中。

Below i have a string which represents a single row pulled from a csv file. Each column is separated by a comma and the value is wrapped in "". What is the simplest way to parse out the value from each column in 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文件。已经有了这个部分。

The above data is all one big string that needs to be split into columns so i can get the values. I'm not asking how to load a csv file. Already got that part figured out.

推荐答案

Python有一个模块:

Python has a module for that:

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

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

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