複数の.txtファイルを1つのファイルにマージしようとしています。一部のファイルは、事前に作成したプレーンテキストの.txtファイルで、一部はスクリプト自体によって書き込まれたファイルです。スクリプトは、ファイルの一部(前もって書き込んだファイル)を正常にマージしますが、エラーは発生しませんが、スクリプトによって作成されたファイルは含めません。それは文字通り彼らの存在を無視しているようです。Pythonで異なるエンコーディングの.txtファイルをマージできません
私はshutil
とfileinput
を含め、さらにsubprocess.call
からではなく、運なしcat
を使用してさまざまな方法を、試してみました。
端末から、私が事前に作成したファイルは「XML文書テキスト」です(実際にはプレーンテキストとして保存しましたが、XMLとしてフォーマットされています)。 XMLファイルは.rtfファイルであり、変換前にマージしないため、これが問題であると思います。リストの最初のものだけが出力ファイルに含まれます。
import os, itertools, subprocess, fileinput, shutil
os.chdir('/Users/MicTonutti/Dropbox/MRes/Individual Project/FEBio/Simulation')
forces = itertools.permutations([-1.5,-1,-0.5,0],3)
forces = list(forces)
force_node = 174
for i in range(0,len(forces)):
filename = '0_Insert' + str(i) + '.txt'
f = open(filename, 'w')
string_x = '<nodal_load bc="x" lc="2"> <node id="' + str(force_node) + '">' + str(forces[i][1]) + '</node> </nodal_load>\n'
string_y = '<nodal_load bc="y" lc="2"> <node id="' + str(force_node) + '">' + str(forces[i][2]) + '</node> </nodal_load>\n'
string_z = '<nodal_load bc="z" lc="2"> <node id="' + str(force_node) + '">' + str(forces[i][2]) + '</node> </nodal_load>\n'
f.write(string_x + string_y + string_z)
filename_2 = '1_Insert' + str(i) + '.txt'
g = open(filename_2, 'w')
string_1 = '<logfile>\n <node_data data="x;y;z" file = "coord_data' + str(i) + '.txt" delim=", "> </node_data>\n'
string_2 = '<node_data data="ux;uy;uz" file = "displacement_data' + str(i) + '.txt" delim=", "> </node_data>\n </logfile>\n'
g.write(string_1 + string_2)
files_list = ['Simulation 1.txt', filename, 'Simulation 2.txt', filename_2, 'Simulation 3.txt']
output_file = '/Users/MicTonutti/Dropbox/MRes/Individual Project/FEBio/Simulation/Python Output/FEBio Simulation Output' + str(i) + '.txt'
with open(output_file, 'w') as outfile:
for infile in files_list:
shutil.copyfileobj(open(infile), outfile)
出力は基本的に次のようになります。途中でファイルのない 『Simulation3.txt「のSimulation2.txt +テキスト』の「Simulation1.txt」+テキストの
テキスト、 どれでもアイデア?
感謝。
はい、申し訳ありません - コピーした後、コードのフォーマットが間違っています。 forループには、最後まですべてが含まれます。 とにかく、flush()は完全に機能します!本当にありがとう。 – michetonu