【发布时间】:2012-04-02 13:35:36
【问题描述】:
我正在使用以下代码在 Python 中导入数据:
one=[]
two=[]
three=[]
four=[]
five=[]
six=[]
x=0
for line in open('text.txt', 'r'):
if x==2:
column0, column1, column2, column3, column4, column5 = line.split(',')
else:
column0, column1, column2, column3, column4, column5 = line.split(' ')
one.append(column0)
two.append(column1)
three.append(column2)
four.append(column3)
five.append(column4)
six.append(column5)
x=x+1
此代码导入此文本文件:
1 2 3 4 5 6
1 2 3 4 5 6
1,2,3,4,5,6
1 2 3 4 5 6
1 2 3 4 5 6
但是我在如何导入以下内容时遇到了很多麻烦
1 2 3 4 5 6
1 2 3 4 5 6
1,2,3,
4,5,6
1 2 3 4 5 6
1 2 3 4 5 6
即使数据在第三行有中断,我希望它以与第一个文本文件相同的方式导入。我尝试按行导入,然后使用第三行的变量数,但我无法让它工作。
有没有人知道任何资源或示例或可以提供帮助?谢谢!
【问题讨论】:
-
是否总是应该每组有六个项目
-
我想我明白他在问什么。他想将每 6 个元素加载到相应的数组中。
-
每六个元素,而不是每六个元素。
标签: python import line-breaks