简体   繁体   English

Python-多个在txt文件中写同一行

[英]Python-Multiple writing the same line in txt file

I have code like this: 我有这样的代码:

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))

Now I would like to my "transakcije.txt" looks like this: 现在我想让我的“ transakcije.txt”看起来像这样:

1:3:iznos:17.06.2017.:username

But it always append the same line for three times. 但是它总是将同一行附加三遍。 With any other kind of indentation it won't append at all. 使用任何其他类型的缩进,它根本不会附加。

Also, my uredjaji.txt file looks like this: 另外,我的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: "username" should be variable from another function, so if someone could help me how to write that variable in this file I will be so thankfull. PS:“用户名”应该是另一个函数的变量,因此,如果有人可以帮助我如何在该文件中写入该变量,我将非常感谢。 :) :)

When you open the file, you read a single line and then go into a while loop on the existence of line . 打开文件时,您读了一行,然后在存在line进入while循环。 If you do not get a match on the input code, you then attempt to, I suppose, reposition the file pointer with f.tell() but you do not do a seek. 如果您在输入代码上没有找到匹配项,那么我想尝试使用f.tell()重新定位文件指针,但是您不执行查找。 Thereafter, you read the file again and write transakcije.txt . 之后,您再次读取该文件并写入transakcije.txt Sadly, the original while loop is still in play, so you will write transakcije.txt multiple times. 遗憾的是,原始的while循环仍在起作用,因此您将多次编写transakcije.txt
It is not clear what you are attempting to achieve with this code but you need to sit down and rethink it from the ground up. 目前尚不清楚您试图用此代码实现什么,但是您需要坐下来重新思考。
If it is some sort of stock reporting/replenishment routine, I can't help thinking that a database (sqlite3 as a simple starter) would be more appropriate that pulling ascii files apart. 如果这是某种库存报告/补货例程,我不禁会认为数据库(作为简单启动器的sqlite3)比将ascii文件分开更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM