简体   繁体   English

将数字写入 txt 文件的第二行,并在 Python 中添加数字

[英]Writing numbers to the second line of txt file wtih adding number on it in Python

I have a script that writes the number at the end of request.url but I need to write it to first line of txt then I want to write another number to the second line of this txt by adding 50000 to the number我有一个脚本,它在 request.url 的末尾写入数字,但我需要将它写入 txt 的第一行,然后我想通过将 50000 添加到数字中,将另一个数字写入该 txt 的第二行

Here is my script:这是我的脚本:

f = open('numbers.txt', 'w')
            f.write(urlparse(request.url).path.split('/')[-1])

An example txt file that I want to make:我要制作的示例 txt 文件:

6100098 <<< this is written from request.url
6150098 <<< this must be written from adding 50000 more

This might work if you need only those 2 number.如果您只需要这 2 个号码,这可能会奏效。 Also I recommend you to add additional checks to make sure you got the correct value from the request.url.此外,我建议您添加额外的检查,以确保您从 request.url 中获得了正确的值。

with open('numbers.txt', 'w') as f:
    initial_value = urlparse(request.url).path.split('/')[-1]
    f.write(initial_value + '\n')
    f.write(str(int(initial_value) + 50000))

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

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