如何在逗号上拆分一行,忽略引号内的逗号?

时间:2016-03-25 10:15:25

标签: python string split

我有一个包含许多行的文件:

1,50,"asasd",25
2,51,"apa,asdfi",26
.....
.....

如何用逗号分隔第二行,跳过双引号内字符串内的逗号?

我需要第二行的结果为:

[2,51,"apa,asdfi",26]

目前我正在尝试:

x = line.split(',')

结果如下:

['2','51','"apa','asdfi"','26']

3 个答案:

答案 0 :(得分:3)

如评论中所述,尝试将您的文件读作.csv,它会对字符串中的昏迷做得很好。 如果您在使用它时遇到问题,可以阅读文档https://docs.python.org/2/library/csv.html orsee中的一些示例https://dzone.com/articles/python-101-reading-and-writing

import csv

with open(file.csv,'r') as f:
    spamreader = csv.reader(f,delimiter=",")
    for row in spamreader:
        # a row is a list containing all elements in a line
        print row

结果:

['1','50','"asasd"','25']  
['2','51','"apa,asdfi"','26']

答案 1 :(得分:0)

您可以尝试以下代码:

line = '2,51,"apa,asdfi",26'
result = line.split(",")

length = len(result)
for i in range(length):
    if '"' in result[i]:
        result[i] += "," + result[i+1]
        result.remove(result[i+1])
        break

length = len(result)
for i in range(length):
    if result[i].isdigit():
        result[i] = int(result[i])

print(result)

<强>输出:

[2, 51, '"apa,asdfi"', 26]

答案 2 :(得分:0)

您也可以使用re.findall

提取它们
>>> s = '2,51,"apa,asdfi",26'
>>> 
>>> re.findall(r'(\d+|".*")', s)
['2', '51', '"apa,asdfi"', '26']

虽然我推荐dup

中提到的方法