在逗号上拆分字符串但忽略双引号内的逗号? [英] Split string on commas but ignore commas within double-quotes?

查看:30
本文介绍了在逗号上拆分字符串但忽略双引号内的逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些如下所示的输入:

I have some input that looks like the following:

A,B,C,"D12121",E,F,G,H,"I9,I8",J,K

逗号分隔的值可以按任何顺序排列.我想用逗号分割字符串;但是,在某些东西在双引号内的情况下,我需要它同时忽略逗号并去掉引号(如果可能).所以基本上,输出将是这个字符串列表:

The comma-separated values can be in any order. I'd like to split the string on commas; however, in the case where something is inside double quotation marks, I need it to both ignore commas and strip out the quotation marks (if possible). So basically, the output would be this list of strings:

['A', 'B', 'C', 'D12121', 'E', 'F', 'G', 'H', 'I9,I8', 'J', 'K']

我看过其他一些答案,我认为正则表达式是最好的,但我很难想出它们.

I've had a look at some other answers, and I'm thinking a regular expression would be best, but I'm terrible at coming up with them.

推荐答案

Lasse 是对的;它是一个逗号分隔值文件,因此您应该使用 csv 模块.一个简单的例子:

Lasse is right; it's a comma separated value file, so you should use the csv module. A brief example:

from csv import reader

# test
infile = ['A,B,C,"D12121",E,F,G,H,"I9,I8",J,K']
# real is probably like
# infile = open('filename', 'r')
# or use 'with open(...) as infile:' and indent the rest

for line in reader(infile):
    print line
# for the test input, prints
# ['A', 'B', 'C', 'D12121', 'E', 'F', 'G', 'H', 'I9,I8', 'J', 'K']

这篇关于在逗号上拆分字符串但忽略双引号内的逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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