我使用下面的代码在Python中导入数据: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
即使数据在第三行有一个中断我希望它导入与第一个文本相同的方式文件中。我尝试按行导入,然后使用第三行的变量数量,但我无法让它工作。
有谁知道任何资源或示例或可以帮助吗?谢谢!
总是应该有六个项目每套 – corn3lius 2012-03-17 03:14:15
我想我明白他在问什么。他希望将每第六个元素加载到相应的阵列中。 – Matt 2012-03-17 03:20:00
每六个元素,不是每六个元素。 – 2012-03-17 03:32:54