0
一些我有一個這樣的製表符分隔的文件替換丟失的數據,與蟒蛇
Acc Pop snp1 snp2 snp3 snp4 snp5
a1 pop1 0 1 0 1 0
a2 pop1 0 1 0
a3 pop1 0 1 0 0 0
a4 pop1 0 1 0 1 0
a5 pop1 0 1 0 0
a6 pop1 1 0 0 0
a7 pop1 0 1 0 0 0
a8 pop1 0 1 0 0 0
a9 pop1 0 1 0
a10 pop1 0 1 0 0 1
我需要替換所有丟失的數據「-9」。因此輸出看起來像這樣,
Acc Pop snp1 snp2 snp3 snp4 snp5
a1 pop1 0 1 0 1 0
a2 pop1 0 1 -9 -9 0
a3 pop1 0 1 0 0 0
a4 pop1 0 1 0 1 0
a5 pop1 0 1 0 -9 0
a6 pop1 -9 1 0 0 0
a7 pop1 0 1 0 0 0
a8 pop1 0 1 0 0 0
a9 pop1 0 1 0 -9 -9
a10 pop1 0 1 0 0 1
這是我下面的嘗試,
import re
infilename = 'file2.txt'
outfilename = 'file.txt'
regex = re.compile(r"\s+")
with open(infilename, 'r') as infile, open(outfilename, 'w') as outfile:
for line in infile:
line = line.rstrip('\n').split('\t')
outfile.write(regex.sub('-9', line))
請將您遇到的錯誤添加到您的問題中。 (在你的情況下:'TypeError:預期的字符串或緩衝區')。 – BioGeek 2015-03-31 09:13:53