Python行的字符串到行列表

时间:2014-01-03 20:40:22

标签: python list

我需要有关格式化字符串行的帮助:

SNUMBER,FULL_ADDRE
822,822 E ASH AVE
1012,1012 BRIAR CREEK RD
1026,1026 N MADISON ST

列出如下:

['SNUMBER', 'FULL_ADDRE']
['822', '822 E ASH AVE']
['1012', '1012 BRIAR CREEK RD']
['1026', '1026 N MADISON ST']

list(self.new_dbf) # breaks this down to a million peaces

5 个答案:

答案 0 :(得分:1)

使用csv

import csv

txt='''\
SNUMBER,FULL_ADDRE
822,822 E ASH AVE
1012,1012 BRIAR CREEK RD
1026,1026 N MADISON ST'''


for line in csv.reader(txt.splitlines()):
    print line

打印:

['SNUMBER', 'FULL_ADDRE']
['822', '822 E ASH AVE']
['1012', '1012 BRIAR CREEK RD']
['1026', '1026 N MADISON ST']

答案 1 :(得分:0)

根据输入格式,string.split(',')可能适合您。 http://docs.python.org/2/library/stdtypes.html#str.split

答案 2 :(得分:0)

import functools
text = """SNUMBER,FULL_ADDRE
822,822 E ASH AVE
1012,1012 BRIAR CREEK RD
1026,1026 N MADISON ST
"""
p=functools.partial(str.split,sep=",")
matrix1 = map(p,text.splitlines()

答案 3 :(得分:0)

>>> lines = open('addresses').readlines() 
>>> [ line.strip().split(',', 1) for line in lines]

答案 4 :(得分:0)

>>> a = '''SNUMBER,FULL_ADDRE
822,822 E ASH AVE
1012,1012 BRIAR CREEK RD
1026,1026 N MADISON ST'''
>>> [a.split(',') for a in a.split('\n')]
[['SNUMBER', 'FULL_ADDRE'], ['822', '822 E ASH AVE'], ['1012', '1012 BRIAR CREEK RD'], ['1026', '1026 N MADISON ST']]