使用Python将特定行转换为列表

时间:2016-07-08 05:13:08

标签: python

我试图占用200行并将每组10个转换为自己的列表。

1
Victorious Boom
834
7
0
7.00
1
0
1.00
1
2
Tier 1 Smurf
806
4
0
4.00
1
0
1.00
1
3
AllHailHypnoToad
754
4
0
4.00
1
0
1.00
1

我想看起来像:

1 Victorious Boom 834 7 0 7.00 1 0 1.00 1
2 Tier 1 Smurf 806 4 0 4.00 1 0 1.00 1
3 AllHailHypnoToad 754 4 0 4.00 1 0 1.00 1

非常感谢任何帮助

3 个答案:

答案 0 :(得分:1)

full_list = [line.strip() for line in open("filename", 'r')] #read all lines into list
sublist = [full_list[i:i+10] for i in range(0, len(full_list), 10)]  #split them into sublist with 10 lines each

答案 1 :(得分:0)

count=0
fixed_list=[]
temp_list=[]
for line in open("some.txt").readlines():
    count+=1
    temp_list.append(line.strip())
    if (count%10)==0:
        fixed_list.append(temp_list)
        temp_list=[]
print fixed_list

答案 2 :(得分:0)

这是我的答案。 它采用逐行类型数据的source.txt,并将数据为10的数据输出到target.txt文件中。我希望这会有所帮助。

file  = open("source.txt", "r")
data = []
for line in file:
    data.append(line)
length = len(data)
file.close()

#output file
target = open("target.txt", "w")

#will become a line in the file
item = ""

if length % 10 == 0:
    for y in range(0, length, 10): 
        for x in range(0, 10):
            item += str(data[x + y].strip()) + " "
        target.write(item + "\n")
        item = ""   
else:
    print ("Bad data set. File "+ str(length) + " elements!")