2016-05-31 44 views
0

我有一個關於一些人口(列和行)的大文件。我想閱讀文件並提取一些信息並編寫腳本爲每個人羣創建一個文件。文件名是可變的 - 不寫入文件

現在,當我想和每一個人口文件「變量名」 只是大文件的最後一行問題被寫入正確的人口。 其他的文件爲空。當我嘗試打印到終端時,它會正確打印數據。

*這些文件已經在代碼的前一部分中創建。

有什麼建議嗎?只有當存儲在「項目」的名稱是相同的存儲在「流行」的一個

for item in d: # d is a set to hold the populations names 
    filename= item # hold the file name 
    if item == 'population': # exclude the header of the column 
     continue 
    elif item== pop: 
     with open(filename+'.txt', 'a') as f: # add to the specific population file 
      f.write(str(fID) +" "+ sID) # write the two columns to the file 
+0

你應該嘗試加入一些打印信息,看看是否是你期望發生實際發生在所有... –

+0

你能告訴我們變量pop是什麼?也許這是要成爲字符串?! –

+3

請使用該語言的正確評論字符。在Python代碼示例中看到C++註釋令人迷惑。 – SethMMorton

回答

0

您的代碼將寫入文件。也許你想生成多個文件這樣的代碼,也許應該是:

if item == 'population': # exclude the header of the column 
    continue 
else: 
    with ...