0

假设我有一个文件,其中包含一行数据,格式如下:

"THESE","ARE","WORDS","AND","I","NEED","THEM","IN","A","LIST"

(当然,实际上文件要大得多。)

有没有一种简单、简短的方法来获取这些数据并将其读入一个可以打印的列表中......

["THESE", "ARE", "WORDS", "AND", "I", "NEED", "THEM", "IN", "A", "LIST"]

...?

我只是想知道,因为虽然肯定可以遍历文件中的每个字符来确定它的含义和它所属的位置,但我觉得这很烦人,而且我确信有一些更简单的方法。我只是不知道方法。

提前致谢!

4

5 回答 5

3
import StringIO
import csv

s = '"THESE","ARE","WORDS","AND","I","NEED","THEM","IN","A","LIST"'
result = csv.reader(StringIO.StringIO(s)).next()

返回

['THESE', 'ARE', 'WORDS', 'AND', 'I', 'NEED', 'THEM', 'IN', 'A', 'LIST']
于 2012-07-13T22:02:07.087 回答
2

如果你不介意一点点后期处理...

>>> ast.literal_eval('"THESE","ARE","WORDS","AND","I","NEED","THEM","IN","A","LIST"')
('THESE', 'ARE', 'WORDS', 'AND', 'I', 'NEED', 'THEM', 'IN', 'A', 'LIST')
于 2012-07-13T21:58:51.397 回答
2

拆分和剥离

a='"THESE","ARE","WORDS","AND","I","NEED","THEM","IN","A","LIST"'
[i.strip('"') for i in a.split(",")]
['THESE', 'ARE', 'WORDS', 'AND', 'I', 'NEED', 'THEM', 'IN', 'A', 'LIST']

请注意,' 和 " 之间没有语义差异。

更新:我支持@IgnacioVazquez-Abrams 对于任何需要接受此类案例的情况的回答:

a=r""""And",'b',"W,B'" """  
ast.literal_eval(a)    # gives the expected and correct
('And', 'b', "W,B'")

而我建议的拆分/剥离解决方案给出了

['And', "'b'", 'W', 'B\'" ']
于 2012-07-13T22:05:04.283 回答
1

为什么不“分裂”?

>>> '"THESE","ARE","WORDS","AND","I","NEED","THEM","IN","A","LIST"'.split(',')
['"THESE"', '"ARE"', '"WORDS"', '"AND"', '"I"', '"NEED"', '"THEM"', '"IN"', '"A"', '"LIST"']

或者

>>> [s.strip('"') for s in '"THESE","ARE","WORDS","AND","I","NEED","THEM","IN","A","LIST"'.split(',')]
['THESE', 'ARE', 'WORDS', 'AND', 'I', 'NEED', 'THEM', 'IN', 'A', 'LIST']

或者

>>> map(lambda s: s.strip('"'), '"THESE","ARE","WORDS","AND","I","NEED","THEM","IN","A","LIST"'.split(','))
['THESE', 'ARE', 'WORDS', 'AND', 'I', 'NEED', 'THEM', 'IN', 'A', 'LIST']
于 2012-07-13T22:00:52.237 回答
1

也许是 hack,但它看起来类似于 JSON

import json
print json.parse('[' + your_line_here + ']')
于 2012-07-13T22:03:01.127 回答