我正在尝试替换列名,然后将新命名的列与其他数据一起写入新数据。以下是数据框的示例
ID 1KS 2861 2862
TP10 0 0 0
TP22 NA 2 2
TP27 0 0 0
TP33 1 0 0
TP34 0 0 0
我希望新文件是这个
ID 1KS_Keizer_F_OR 2861_Lassen_NF_CA 2862_Lassen_NF_CA
TP10 0 0 0
TP22 NA 2 2
TP27 0 0 0
TP33 1 0 0
TP34 0 0 0
但是使用我的脚本,我看到一行空行,如下所示
ID 1KS_Keizer_F_OR 2861_Lassen_NF_CA 2862_Lassen_NF_CA
TP10 0 0 0
TP22 NA 2 2
TP27 0 0 0
TP33 1 0 0
TP34 0 0 0
这是我的剧本..
import sys
result = {}
if len(sys.argv) != 4:
print "python" + " " + sys.argv[0] + " " + "<rename file>" + " " + "<input file>" + " " + "<output file>"
sys.exit()
file_in = sys.argv[1]
file_in2 = sys.argv[2]
file_out = sys.argv[3]
def main():
fh_in = open(file_in, 'r')
for line in fh_in:
line = line.strip().split("\t")
result[line[0]] = line[1]
with open(file_in2, 'r') as fh_in2:
with open(file_out, 'a') as fh_out:
for line2 in fh_in2:
if line2.startswith("ID"):
line2 = line2.split()
for i in line2:
new = result[i]
fh_out.write(new)
fh_out.write("\t")
else:
fh_out.write("\n")
fh_out.write(line2)
fh_in.close()
main()