Python,repl.it-无法使用csv.writer和writer.writerow将详细信息写入文件 [英] Python, repl.it - details are not being written to file using csv.writer and writer.writerow

查看:90
本文介绍了Python,repl.it-无法使用csv.writer和writer.writerow将详细信息写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下repl.it程序,请注意,该程序的注册部分之前运行良好,已经停止工作.

直到最后说写到文件".但是以某种方式跳过了实际的写行命令,因为没有任何内容写入文本文件.

整个程序在这里:

https://repl.it/@oiuwdeoiuas/Matchmakingskills-1

下面是代码的相关部分,尽管可能还有其他因素(因此提供了完整的代码)

  def寄存器():打印("===注册====")打印(首先,请先注册并向我们介绍一些您自己的情况")将open("dating.txt","a")设为:writer = csv.writer(fo)firstname = input(输入名字:")lastname = input(输入姓氏:")username = firstname + lastname [0] +"bird"打印(您自动生成的用户名是:",用户名)password = input(输入密码:")性别=输入(输入性别")email = input(输入电子邮件:")dob = input(以dd/mm/yy格式输入出生日期:")信念=输入(输入信念")strengthslist = [耐心",效率",敏感性",坦率",顺从",领导",计时",悠闲"]打印(强度列表)strengths = input(输入您的最高强度:(从上面的列表中选择)")contactcount = 0writer.writerow([用户名,密码,名字,姓氏,性别,电子邮件,任务,信仰,实力,联系方式])打印(写入文件")主菜单() 

解决方案

您正在尝试读取仍打开的文件:

  def register():#...将open("dating.txt","a")设为:#...打印(写入文件")#至此,"dating.txt"尚未被写入#在此处发生的下一个打开它的调用将#查看部分写入文件状态或之前的文件状态#该行已全部写入主菜单() 

有一些解决方案.最快的方法是在一个级别上取消 mainmenu()的缩进:

  def register():#...将open("dating.txt","a")设为:#...打印(写入文件")#约会.txt现已关闭,可以安全阅读主菜单() 

当下一个方法出现并尝试读取文件时,它将以这种方式包含预期的数据.

I have the following repl.it program, and note that the registration part of the program, that was working fine before, has stopped working.

It gets to the end where it says "written to file" but somehow the actual write-rows command is being skipped as nothing is written to the text file.

The whole program is here:

https://repl.it/@oiuwdeoiuas/Matchmakingskills-1

The relevant part of the code is below, although there may be other factors (hence whole code provided)

def register():
    print("===Register====")
    print("First things first, sign up and tell us a little about yourself")
    with open("dating.txt","a") as fo: 
        writer=csv.writer(fo)        
        firstname=input("Enter first name:")
        lastname=input("Enter last name:")
        username=firstname+lastname[0]+"bird"
        print("Your automatically generated username is:",username)
        password=input("Enter password:")
        gender=input("Enter gender")
        email=input("Enter email:")
        dob=input("Enter date of birth in format dd/mm/yy:")
        beliefs=input("Enter beliefs")
        strengthslist=["patience","efficiency","sensitivity","frankness","submissiveness","leadership","timekeeping","laidback"]
        print(strengthslist)
        strengths=input("Enter your top strength: (select from the above list)")
        contactcount=0
        writer.writerow([username,password,firstname,lastname,gender,email,dob,beliefs,strengths,contactcount])
        print("written to file")
        mainmenu()

解决方案

You're attempting to read a file that's still open:

def register():
    # ...
    with open("dating.txt","a") as fo: 
        # ...
        print("written to file")
        # At this point, "dating.txt" hasn't been written to
        # the next call to open it that occurs here will
        # see the state of the file either partially written, or before
        # the row is written at all
        mainmenu()

There are a few solutions. The quickest is to de-indent mainmenu() here one level:

def register():
    # ...
    with open("dating.txt","a") as fo: 
        # ...
        print("written to file")
    # dating.txt has been closed now, it's safe to read it
    mainmenu()

When the next method comes along and tries to read the file, it will contain the expected data that way.

这篇关于Python,repl.it-无法使用csv.writer和writer.writerow将详细信息写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆