附加文件时删除新线路

发布于 2025-02-01 01:01:44 字数 1677 浏览 1 评论 0原文

我有一个问题,当我附加到文件时,始终出于某种原因始终添加新线。这打破了我的代码。它是某种登录系统。我已经搜索了很多答案。目前,我找到了.rstrip('\ r').strip('\ t') and .strip('\ n')没有工作。

示例:

时,我将其写入这样的文件

print("Create")
with open('database.txt', 'a') as database:
    username = input("Enter a username: ")
    password = input("Enter a password: ")
    combined = (password + "," + username + "<>")
    combined = combined.strip('\n')
    combined = combined.strip('')
    combined = combined.strip(' ')
    combined = combined.rstrip('\r')
    combined = combined.strip('\t')
    database.write(combined)
    database.close()

    #checking total number of users
    with open('usernum.txt', 'r+') as usernum:
        #the current number + 1 will be the number of the new user
        total_num_of_users = usernum.read()
        total_num_of_users = int(total_num_of_users) + 1
        #incrementing for future numbers
        total_num_of_users = str(total_num_of_users)
        usernum.truncate(0)
        usernum.write(total_num_of_users)
        usernum.close()

    #making a file containing the users place/number
    namefile = str(username) + '.txt'
    with open(namefile, 'w+') as usernum:
        usernum.write(total_num_of_users)
        usernum.close()

当它已经具有内容password1,username1&lt;&gt;:请忽略文件称为database.txt lol 。如果我的输入是username2password2我的预期输出是:

password1,username1&lt;&gt; password2,username2&lt;&gt;

但是我得到了:

password1,username1&lt;&gt;

password2,username2&lt;&gt;

谁能帮我这个?提前致谢

I have an issue where when I append to a file, a newline is always added for some reason. This breaks my code. It is meant to be some sort of login system. I have searched for answers alot. Currently I have found the .rstrip('\r'), .strip('\t') and .strip('\n') have not worked.

Example:

I write to a file like this when it already has the contents password1,username1<>:

print("Create")
with open('database.txt', 'a') as database:
    username = input("Enter a username: ")
    password = input("Enter a password: ")
    combined = (password + "," + username + "<>")
    combined = combined.strip('\n')
    combined = combined.strip('')
    combined = combined.strip(' ')
    combined = combined.rstrip('\r')
    combined = combined.strip('\t')
    database.write(combined)
    database.close()

    #checking total number of users
    with open('usernum.txt', 'r+') as usernum:
        #the current number + 1 will be the number of the new user
        total_num_of_users = usernum.read()
        total_num_of_users = int(total_num_of_users) + 1
        #incrementing for future numbers
        total_num_of_users = str(total_num_of_users)
        usernum.truncate(0)
        usernum.write(total_num_of_users)
        usernum.close()

    #making a file containing the users place/number
    namefile = str(username) + '.txt'
    with open(namefile, 'w+') as usernum:
        usernum.write(total_num_of_users)
        usernum.close()

Please ignore the fact that the file is called database.txt lol. If my input is username2 and password2 my expected output is:

password1,username1<>password2,username2<>

However instead I get:

password1,username1<>

password2,username2<>

Can anyone help me wth this? Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文