0
我有以下視圖中的文件數量的文件創建塊:的Python:基於行
ID Num1 Num2
1 1 2
1 2 4
1 3 6
2 4 8
2 5 10
3 6 12
3 7 14
3 8 16
3 9 18
4 10 20
5 11 22
5 12 24
我想拆基於每個文件不應該包含條件的文件分成多個文件的更多超過3個ID值。所以,在這種情況下,他們會輸出兩個文件。第一個文件應該有輸出:
1 1 2
1 2 4
1 3 6
2 4 8
2 5 10
3 6 12
3 7 14
3 8 16
3 9 18
雖然第二個文件應該是這樣的:
4 10 20
5 11 22
5 12 24
但相反,它會產生,三個文件有第二個文件作爲一個空白文件,包含值3文件只有ID = 5。
下面的代碼是用Python編寫的:
infile=open("Input.txt","r")
outfile=open("Output1.txt","w")
list_value=[] # Storing the values of 1st column
file=2 # Naming the file
value="" # 1st column value
for line in infile:
value=line.split()[0]
if value in list_value:
outfile.write(line)
else:
list_value.append(value)
if (len(list_value)) < 4:
outfile.write(line)
elif (len(list_value))==4:
outfile=open("Output"+str(file)+".txt","w")
outfile.write(line)
file=file+1
list_value=[]
的代碼僅對第一序列工作正常,但未能在結束工作。
對於Linux的任何命令也可以是有幫助的。
謝謝!在你的代碼
您認爲ID是在輸入文件中排序? – Shai
你應該正確'.close()'你的文件。 –
是ID在文件中排序。 – AsmaTahir