私はこのようなテキストファイルを持っています:-
V1xx AB1
V2xx AC34
V3xx AB1
;
Python スクリプトを使用して行末に追加できますか?
V1xx AB1;
V2xx AC34;
V3xx AB1;
私はこのようなテキストファイルを持っています:-
V1xx AB1
V2xx AC34
V3xx AB1
;
Python スクリプトを使用して行末に追加できますか?
V1xx AB1;
V2xx AC34;
V3xx AB1;
これがあなたが試すことができるものです。私は持ってoverwritten the same file
います。
できますtry creating a new one
(お任せします) -with
ステートメントを少し変更する必要があります : -
lines = ""
with open('D:\File.txt') as file:
for line in file:
lines += line.strip() + ";\n"
file = open('D:\File.txt', "w+")
file.writelines(lines)
file.flush()
更新: -ファイルのインプレース変更fileinput
には、モジュールを使用できます: -
import fileinput
for line in fileinput.input('D:\File.txt', inplace = True):
print line.strip() + ";"
input_file_name = 'input.txt'
output_file_name = 'output.txt'
with open(input_file_name, 'rt') as input, open(output_file_name, 'wt') as output:
for line in input:
output.write(line[:-1]+';\n')