pycharm Python循环并添加到字典中,直到文件中出现空行

9gm1akwq  于 2022-11-23  发布在  PyCharm
关注(0)|答案(5)|浏览(173)

我尝试从文件中添加位置、名称、教室,直到空白,使用:
获取. txt格式的内容|school.py
.txt格式的示例:

supervisor
Jack
1a
1b
2a
supervisor_list = []
supervisor_dict = {}
def user_type():
    check_who = input()
    if check_who == "supervisor":
        name = input()
        check_class = input()
        supervisor_dict["User"] = check_who
        supervisor_dict["Name"] = name
        supervisor_dict["School_class"] = check_class
        check_class = input()
        while not check_class.isspace():
            supervisor_dict["School_class"] += " " + check_class
            supervisor_list.append(supervisor_dict)
            check_class = input()

你能告诉我我做错了什么吗?2谢谢!

uqxowvwt

uqxowvwt1#

check_class.isspace()检查字符串是否为空格,如““。如果要检查某行是否为空,只需使用check_class == '',或者,如果要将白色的行视为空行,则使用check_class.strip() == ''

wd2eg0qa

wd2eg0qa2#

代码check_class.isspace()将检查字符串是否为空''如果要检查行是否为空,可以尝试check_class == '';如果要检查字符串中是否包含除(' ' in check_class) == True以外的任何空格

kzmpq1sx

kzmpq1sx3#

首先你应该运行函数。2如果你想让用户通过命令行输入数据,这是一个可行的解决方案:

supervisor_list = []
supervisor_dict = {}
stop_character = ""

def user_type():
    check_who = input()
    if check_who == "supervisor":
        name = input()
        check_class = input()
        supervisor_dict["User"] = check_who
        supervisor_dict["Name"] = name
        supervisor_dict["School_class"] = check_class
        while True:
            check_class = input()
            if check_class == stop_character:
                break
            supervisor_dict["School_class"] += " " + check_class
        supervisor_list.append(supervisor_dict)

user_type()
kzipqqlq

kzipqqlq4#

您可能希望它这样写

supervisor_list = []
supervisor_dict = {}
def user_type():
    check_who = input("Enter user type: ")
    if check_who == "supervisor" or check_who == "Supervisor":
        name = input("Enter name: ")
        check_class = input("Enter class: ")
        supervisor_dict["User"] = check_who
        supervisor_dict["Name"] = name
        supervisor_dict["School_class"] = check_class
        while not check_class.isspace():
            supervisor_dict["School_class"] += " " + check_class
            check_class = input("Enter class: ")
        supervisor_list.append(supervisor_dict)
    
    return supervisor_list
8iwquhpp

8iwquhpp5#

你可以修改你的terminal参数,让它有一些可以识别的东西,否则你就没有行了。
这将在末尾或空行处停止。

(Get-Content in.txt) + '' | python school.py

school.py

supervisor_list = []
def user_type():
    supervisor_dict = {
        'User': input(),
        'Name': input(),
        'School_class': input()
    }
    if supervisor_dict["User"] == "supervisor":
        while (check_class := input()):
            supervisor_dict["School_class"] += ' ' + check_class
        supervisor_list.append(supervisor_dict)

user_type()
print(supervisor_list)

输出量:

[{'User': 'supervisor', 'Name': 'Jack', 'School_class': '1a 1b 2a'}]

可替代的,如果我们不能改变输入,让我们继续下去,直到出现错误,或者出现空行:

Get-Content in.txt | python school.py

school.py

supervisor_list = []
def user_type():
    supervisor_dict = {
        'User': input(),
        'Name': input(),
        'School_class': input()
    }
    if supervisor_dict["User"] == "supervisor":
        while True:
            try:
                if not (check_class := input()) : break
            except EOFError : break   
            supervisor_dict["School_class"] += ' ' + check_class
        supervisor_list.append(supervisor_dict)  

user_type()
print(supervisor_list)

相关问题