【问题标题】:How can I write input on new lines in files in python, without adding an empty line?如何在不添加空行的情况下在 python 文件的新行上写入输入?
【发布时间】:2017-01-27 00:00:09
【问题描述】:

我想知道,如何让我的代码在文件的新行中添加每个输入,但不添加额外的空行。 目前,我正在使用“\n”将每个输入追加到新行上,但这会增加一个额外的空行。有什么方法可以在不添加额外行的情况下做到这一点?

这是我所有的代码:-

f= open("Passes.py", "a+")
m=open("money.py","r+")
passes= {}
init={}
initial=0
import time
print "Welcome to the virtual banking system"
user=raw_input("Would you like to create a new account? 'Y/N'").lower()
if user== "y":
  new_user= raw_input("Create a username:")
  new_pass= raw_input("Create a password:")
  p= passes[new_user]= new_user + ":" + new_pass
  f.write("\n"+p)
  ask=raw_input("Would you like to sign into your account? 'Y/N'").lower()
  if ask=="y":
    user_in=raw_input("Enter your username:")
    if user_in==new_user:
      pass_in=raw_input("Enter your password:")
      if pass_in==new_pass:
        print "Welcome to your account" + " " + new_user
        useropt=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2, withdraw money- enter 3 or exit- enter 4:")
        if useropt=="1":
          print "Your balance is:", initial
        if useropt=="2":
          amountdep= int(raw_input("How much money would you like to deposit?:"))
          initial+=amountdep
          print "Thanks. Your new balance is:", initial
        if useropt=="3":
          amountwith=int(raw_input("How much would you like to withdraw?:"))
          initial-=amountwith
          print "Your balance is:", initial
        i=init[new_user]=str(initial)
        m.write(str(i)+"\n")
      else:
        print "Password not valid"

    else:
      print "Username does not exist"

  else:
    print "Thanks for using the virtual bank."

else:
  user2=raw_input("Do you have an existing account? 'Y/N'").lower()
  if user2=="y":
    existing_user=raw_input("Enter your username:")
    exisitng_pass=raw_input("Enter your password:")
    for passwords in f:
      if passwords==existing_user+":"+exisitng_pass:
        print "Welcome to your account" + " " + existing_user
        useropt2=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2, withdraw money- enter 3 or exit- enter 4:")
        if useropt2=="1":
          for info in m:
            print "Your balance is:", info
        if useropt2=="2":
          amountdep= int(raw_input("How much money would you like to deposit?:"))
          for info in m:
            a=int(info)+int(amountdep)
            print "Thanks. Your balance is:", int(a)
          m.close()
          m= open("money.py", "w")
          m.write(str(a))
        if useropt2=="3":
          amountwith=int(raw_input("How much would you like to withdraw?:"))
          for info in m:
            t=int(info)-int(amountwith)
            print "Thanks. Your balance is:", t
          m.close()
          m=open("money.py","w")
          m.write(str(t))

【问题讨论】:

  • 如果你使用print,它会自动添加一个换行符,所以不要在你打印的内容中包含"\n"。如果这不能解决您的问题,请发布一些示例代码。
  • 我没有使用打印,但我只是写入文件
  • f.write(p+"\n")
  • 你能把你用来写的代码和输入代码贴出来吗?
  • 也许您的数据末尾已经有'\n',而您忘记了strip()。顺便说一句:当您从文件中读取行时,它不会删除 '\n' 所以您必须手动删除它。

标签: python file-io file-writing


【解决方案1】:

防止尾随换行符的方法是在字符串的开头打印换行符。这显然会在文件的开头引入一个空行。为防止这种情况发生,您可以使用tell() 检查要打印的字符串是否将成为文件中的第一行,如果是则跳过换行符。这应该消除尾随换行符的问题。这看起来像这样:

position = m.tell()
if position == 0:
    m.write(str(i))
else:
    m.write('\n'+str(i))

【讨论】:

    【解决方案2】:

    我相信您需要做的只是:

    m.write(str(i)+"\n")
    

    你想做的事:

    m.write(str(i))
    

    另外,你有一个小错字;当您使用 existing_pass 变量时,您将其拼写为 exisitng_pass,但您的代码会运行,因为它在两次使用时都拼错了。

    【讨论】:

    • 这个问题是我仍然希望文件在新行上写入,我只是不希望最后有一个空行。
    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    相关资源
    最近更新 更多