に同じラインを書いて、私はこのようなコードがあります:のPython-複数のtxtファイル
def export_devices():
code = input("Enter device code: ")
amount = int(input("How many devices you export: "))
with open("uredjaji.txt", "r+") as f:
current_position = 0
line = f.readline()
while line:
if line[:len(code) + 1] == code + ":":
line = line.rstrip()
amount_index = line.rfind(":") + 1
current_amount = int(line[amount_index:])
if amount > current_amount:
print("There no that many devices in stock...")
return
remaining_content = f.read()
f.seek(current_position)
f.truncate()
line = line[:amount_index] + str(current_amount - amount) + "\n"
f.write(line)
f.write(remaining_content)
return
current_position = f.tell()
line = f.readline()
with open('transakcije.txt','a') as transactions:
date = datetime.date.today().strftime('%d.%m.%Y.')
transactions.write("1" + ":" + str(amount) + ":" + "export" + ":" + str(date) + ":" + "username" + "\n")
print("Error device code: {}".format(code))
を今、私は私の「transakcije.txt」への希望は次のようになります。
1:3:iznos:17.06.2017.:username
しかし、常に同じ行を3回追加します。他の種類のインデントでは、それはまったく追加されません。
また、私のuredjaji.txtファイルは次のようになります。
tw004:Galaxy S5:Samsung:Mobilni telefon:3
tw002:Galaxy S6:Samsung:Mobilni telefon:1
tw001:Huawei P8:Huawei:Mobilni telefon:1
tw003:Huawei P9:Huawei:Mobilni telefon:100998
PS:誰かがこのファイルにその変数を作成する方法を私を助けることができるので、もし「ユーザ名が」私は意志、他の関数から変数でなければなりませんとても感謝しています。 :)
あなたは何をしようとしているのか説明できますか?あなたのコードに従うのは難しいです。 – Joshua
現在の出力テキストファイルを投稿できますか?これは問題の検証に役立ちます。 –
マイtransakcije.txtファイルは次のようになります。 '1:3:iznos:2017年6月17日:3:iznos:2017年6月17日: 1名。ユーザ名 1:3:iznos:2017年6月17日。 :username' そして、私はこの関数を使って永遠に1行しか書こうとしていません。 –