如何将文本文件加载到列表中,然后在Python中使用普通列表功能使用该列表?
我的程序遇到了麻烦。我最初将其设置为让用户输入填充嵌套列表的员工信息,然后对该信息进行操作。每个嵌套名单都是一名员工。工作正常。现在,我添加了将输入信息保存为文本文件的功能,也可以从该文件中加载。我相信我们应该将其运行在空闲的外壳中,而不是终端窗口。
我可以将信息保存在文本文件中,然后出现这样的信息:
John Doe,111111111,(555)555-1212,[email protected],$50000
Jane Doe,222222222,(444)555-1111,[email protected],$50,000
我正在加载它:
for line in open('employee_file.txt'):
items = line.rstrip('\n').split('\t')
items = [item.strip() for item in items]
temp_load_list.append(items)
这给我带来了这样的嵌套列表:
[[John Doe,111111111,(555)555-1212,[email protected],$50000],[Jane Doe,222222222,(444)555-1111,[email protected],$50,000]]...
这是我只使用用户输入数据时获得的结构,但用户除外输入的数据除了第二个项目以外的所有报价,因为它是整数。它与我为用户输入的内容编写的所有其他功能都无法使用,所以我想,我会在某些项目周围添加单个引号,并根据需要键入每个字符串或整数...但是我无法在此列表中处理单个项目!
如果我尝试在第一个列表中打印第一个项目,则ID期望“ John Doe”
将返回。
print('This should be first item of first list:',temp_load_list[0][0])
相反,我得到的是:
John Doe,111111111,(555)555-1212,[email protected],$50000
括号消失了,但仅此而已。如果我尝试用for循环分解,我会垂直播放每个字符。因此,我无法更改任何特定项目,也无法挑选出它们以填充显示字段,例如“员工1名称:”
我想拥有一个可以在空闲编辑器环境中使用的解决方案,因此任何导入的任何进口模块必须这样做。
为什么我从文本文件中列出的列表不像列表? 事先感谢您提出的任何建议! 尼尔
I am having trouble with my program. I had originally set it up to have a user enter employee information that populated a nested list, and then do things with that info. Each nested list was one employee. Worked fine. Now I am adding the ability to save the entered info as a text file, and also to load from that file. I believe we are supposed to have it run in the IDLE shell as opposed to a terminal window.
I can save the info in a text file and it comes out like this:
John Doe,111111111,(555)555-1212,[email protected],$50000
Jane Doe,222222222,(444)555-1111,[email protected],$50,000
I am loading it as such:
for line in open('employee_file.txt'):
items = line.rstrip('\n').split('\t')
items = [item.strip() for item in items]
temp_load_list.append(items)
This gets me a nested list like this:
[[John Doe,111111111,(555)555-1212,[email protected],$50000],[Jane Doe,222222222,(444)555-1111,[email protected],$50,000]]...
This is the same structure I got when only using my user entered data, except the user entered data had single quotes around all but the second item since it was an integer. It doesnt work with all of the other functionality that I wrote for the user entered stuff so I thought, well I will add the single quotes around the certain items, and type each as a string or integer as needed...but I can not address a single item in this list!
If I try to print the first item in the first list, Id expect "John Doe"
to be returned.
print('This should be first item of first list:',temp_load_list[0][0])
Instead what I get is:
John Doe,111111111,(555)555-1212,[email protected],$50000
The brackets are gone, but that's it. If I try to break that down with a for loop, I get each character streaming out vertically. So, I am not able to change any of the specific items, or pick them out to populate a display field like "Employee 1 Name:"
I'd like to have a solution that will work in the IDLE editor environment, so any imported modules would have to do that.
Why is the list I make from a text file not behaving like a list?
Thanks in advance for any suggestions!
Neil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论