我想从文件中获取数据,并将其整理成如下形式的元组列表:
[('yes', 31, 'good'), ('yes', 31, 'good'), ('yes', 31, 'good')]
我的文件看起来像这样
问题是当我用换行符(\n
)分割时,结果会在列表中每个字符串后添加一个包含空格的元素,如下输出所示
我在这里所做的是将它分割到一个列表中
我想知道为什么在列表中每个字符串后会得到一个空格作为元素
with open('c:\\users\\Ahmed Zaky\\Desktop\\Ahmed\\Master\\TUB\\PyML\\Lecture 1 15 Oktober - 21 Oktober\\Exercise Sheet 1\\health-test.txt','r') as h_test:D = list()for t in h_test: D.extend([x for x in str.split(t,'\n')])print(D)
输出
['yes,21,poor', '', 'no,50,good', '', 'no,23,good', '', 'yes,45,poor', '', 'yes,51,good', '', 'no,60,good', '', 'no,15,poor', '', 'no,18,good', '']
元组列表看起来像这样
with open('c:\\users\\Ahmed Zaky\\Desktop\\Ahmed\\Master\\TUB\\PyML\\Lecture 1 15 Oktober - 21 Oktober\\Exercise Sheet 1\\health-test.txt','r') as h_test:D = list()for t in h_test: D.extend([(x,) for x in str.split(t,'\n')])print(D)
输出
[('yes,21,poor',), ('',), ('no,50,good',), ('',), ('no,23,good',), ('',), ('yes,45,poor',), ('',), ('yes,51,good',), ('',), ('no,60,good',), ('',), ('no,15,poor',), ('',), ('no,18,good',), ('',)]
回答:
不要用换行符split
,而是先用strip
去除行的空白字符。然后用逗号split
你得到的字符串,并将第二个值转换为int
:
with open(path) as h_test: stripped_lines = (line.strip() for line in h_test) non_empty_lines = (line for line in stripped_lines if line) split_lines = (line.split(',') for line in non_empty_lines) D = [(a, int(b), c) for a, b, c in D]
如果有行不包含三个值,且第二个值不是int
,这将失败。根据您的数据,您可能需要对此进行检查。