2016-10-21 32 views
-1

私はpythonを使っている間にファイルに書き込もうとしていますが、なんらかの理由で私の作成したファイルではなく自分のコンソールに書き込みを続けます。はい、私はこの質問が以前に尋ねられたことを知っています。はい.close()コマンドを使用しました。ここに私のコードブロックがあります。ファイルに書き込む

myfile= open ('C:/Users/12345/Documents/Grouped_data.txt','r') 

with open ('C:/Users/12345/nanostring.txt','w') as output: 

    for line in myfile: 
     Templist= line.split() 
     print line 
     print Templist[0], Templist[4], Templist[5],Templist[6], Templist[7], Templist[8], Templist[9], Templist[10], Templist[12] 
     print output 
myfile.close() 
output.close() 

答えて

0

これは、のように単純でなければなりません:ドキュメントから

>>> with open('somefile.txt', 'a') as the_file: 
...  the_file.write('Hello\n') 

:テキストモード(デフォルト)で開かれたファイルを書き込むとき

は、ラインターミネータとしてos.linesepを使用しないでください;代わりに、すべてのプラットフォームで単一の '\ n'を使用してください。 python 2.7で

0

あなたはprint>>を使用して、それがあるので、ここでas name

を使用することができますprint>>output,line

myfile= open ('C:/Users/12345/Documents/Grouped_data.txt','r') 
with open ('C:/Users/12345/nanostring.txt','w') as output: 
    for line in myfile: 
     Templist= line.split() 
     print>>output,line # Note the changes 
     print>>output,Templist[0], Templist[4], Templist[5],Templist[6], Templist[7], Templist[8], Templist[9], Templist[10], Templist[12] # Note the changes 

注:printは直接端末とprint>>as name,印刷物に印刷しますファイルへ。

関連する問題