0

私はこのようなテキストファイルを持っています:-

V1xx AB1
V2xx AC34
V3xx AB1

;Python スクリプトを使用して行末に追加できますか?

V1xx AB1;
V2xx AC34;
V3xx AB1;
4

3 に答える 3

1

これがあなたが試すことができるものです。私は持って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() + ";"
于 2012-10-16T22:35:13.633 に答える
1
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')
于 2012-10-16T22:40:10.390 に答える