這是很難驗證它是否工作不帶.txt,但給一個嘗試到一個
f = open(os.path.join(data_path, 'chr1.phyloP46way.placental2.wigFix'), 'r')
lines = f.readlines()
num = 10981
for line_num in range(len(lines)):
line_in = lines[line_num]
num = num + 1
print line_in.strip() + '\t' + str(num)
----更新:下面的羅裏Daulton評論
我有一些時間做一個小測試。也許這個人會有所幫助: 保存下面的代碼在一個名爲converter.py
import os
def add_enumeration(data_path, filename_in, filename_out, num=10981):
# compose the filenames:
path_to_file_in = os.path.join(data_path, filename_in)
path_to_file_out = os.path.join(data_path, filename_out)
# check if the input file exists:
if not os.path.isfile(path_to_file_in):
raise IOError('Input file does not exists.')
# open the files:
# if f_out does not exists it will be created.
# if f_out is not empty, content will be deleted
f_in = open(path_to_file_in, 'r')
f_out = open(path_to_file_out, 'w+')
# write the first line of the file in:
f_out.write(f_in.readline())
for line_in in f_in:
f_out.write(line_in.strip() + ' ' + str(num) + '\n')
num = num + 1
f_in.close()
f_out.close()
然後從IPython的終端文件:
In: run -i converter.py
In: add_enumeration('/Users/user/Desktop', 'test_in.txt', 'test_out.txt')
注意,如果TEST_OUT不是空的,它的內容將是刪除。 這應該避免使用readlines()導入列表中的所有行。讓我知道如果內存問題仍然存在。
來源
2016-08-11 11:40:45
SeF
歡迎來到SO!請更正代碼的格式,以便我們不必假定塊有多遠。最簡單的方法通常是粘貼代碼,全選,然後單擊代碼格式按鈕(看起來像'{}')。另外,如果腳本本身崩潰,請提供回溯。 – glibdud