尝试打开像 txt 文件这样的字典并将其内容打印到 Python 中的 PrettyTable
Trying to open a dictionary like txt file and print its contents into PrettyTable in Python
我正在尝试创建一个基于具有键“name”和值“age”的字典的小型 Python 脚本,它应该接受用户输入,将字典写入 .txt 文件并打开然后将文件的内容输出到 Pretty Table 中,列名为“Name”、“Age”。用户键入的所有姓名都应位于“姓名”列下,年龄也应如此。整个代码都在一个 while 循环中,只有在用户键入“退出”时才会关闭。我想要实现的是,当用户键入退出程序时,应该在文件中打印所有以前的姓名和年龄,包括退出程序之前键入的姓名和年龄到 Pretty Table.
这是我的代码:
from prettytable import PrettyTable
import os
sw_dir = "C:\Users\user\Documents\"
dir_change = os.chdir(sw_dir)
test_dict = {}
active = True
name_table = PrettyTable(["Name", "Age"])
filename = "name_dict.txt"
while active:
with open(filename, "a") as a_file:
user_name = input("Enter name: ")
user_age = input("Enter age: ")
user_exit = input("Type quit to exit: ")
test_dict[user_name] = user_age
if user_exit == "quit":
active = False
for name, age in test_dict.items():
a_file.write(str(name) + ":" + str(age) + "\n\n")
with open(filename, "r") as r_file:
for test_dict in r_file:
name_table.add_row([name, age])
print(name_table)
问题是程序只打印出退出前键入的名称(多次),但不打印文件内的文件夹名称,即整个文件。
输出样本:
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
+---------------+-----+
+---------------+-----+
| Name | Age |
+---------------+-----+
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
+---------------+-----+
如果有人能帮助我,我将不胜感激,因为我对 Python 还是个新手,并且已经为这个问题苦苦挣扎了好几天。
有两点需要注意。
首先是字典键只能有一个值,键不能重复。因此,您将无法通过如下所示的字典获得所需的结果:
{
"Grace Johnson": "26",
"Grace Johnson": "26",
#...
"Elliot McLane": "42",
"Elliot McLane": "42",
}
你需要一个更像这样的结构:
[
{"Name": "Grace Johnson", "Age": "26"},
{"Name": "Grace Johnson", "Age": "26"},
# ....
{"Name": "Elliot McLane", "Age": "42"},
{"Name": "Elliot McLane", "Age": "42"},
]
在任何一种情况下,下一个小问题是字典或适当的字典列表确实需要编写完整的文件,如果您只想随时追加,您可能会查看“json行”数据格式。参见:https://jsonlines.org/
无论如何,考虑到这些限制(并忽略 PrettyTable
,因为它似乎与问题无关)这是一种继续进行的方法。
import json
file_name = "name_dict.txt"
## ---------------------------
## Open our datafile (creating it is needs be)
## ---------------------------
with open(file_name, "a+") as a_file:
a_file.seek(0) # reset the pointer back to the start of the file
test_dict = json.loads(a_file.read() or "{}")
## ---------------------------
while True:
if "quit" == input("Type quit to exit: "):
break
user_name = input("Enter name: ")
user_age = input("Enter age: ")
test_dict[user_name] = user_age
## ---------------------------
## Write the complete dictionary back out
## ---------------------------
with open(file_name, "w") as a_file:
json.dump(test_dict, a_file)
## ---------------------------
for key, value in test_dict.items():
print(f"{key} --> {value}")
我正在尝试创建一个基于具有键“name”和值“age”的字典的小型 Python 脚本,它应该接受用户输入,将字典写入 .txt 文件并打开然后将文件的内容输出到 Pretty Table 中,列名为“Name”、“Age”。用户键入的所有姓名都应位于“姓名”列下,年龄也应如此。整个代码都在一个 while 循环中,只有在用户键入“退出”时才会关闭。我想要实现的是,当用户键入退出程序时,应该在文件中打印所有以前的姓名和年龄,包括退出程序之前键入的姓名和年龄到 Pretty Table.
这是我的代码:
from prettytable import PrettyTable
import os
sw_dir = "C:\Users\user\Documents\"
dir_change = os.chdir(sw_dir)
test_dict = {}
active = True
name_table = PrettyTable(["Name", "Age"])
filename = "name_dict.txt"
while active:
with open(filename, "a") as a_file:
user_name = input("Enter name: ")
user_age = input("Enter age: ")
user_exit = input("Type quit to exit: ")
test_dict[user_name] = user_age
if user_exit == "quit":
active = False
for name, age in test_dict.items():
a_file.write(str(name) + ":" + str(age) + "\n\n")
with open(filename, "r") as r_file:
for test_dict in r_file:
name_table.add_row([name, age])
print(name_table)
问题是程序只打印出退出前键入的名称(多次),但不打印文件内的文件夹名称,即整个文件。
输出样本:
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
+---------------+-----+
+---------------+-----+
| Name | Age |
+---------------+-----+
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Grace Johnson | 26 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
| Elliot McLane | 42 |
+---------------+-----+
如果有人能帮助我,我将不胜感激,因为我对 Python 还是个新手,并且已经为这个问题苦苦挣扎了好几天。
有两点需要注意。
首先是字典键只能有一个值,键不能重复。因此,您将无法通过如下所示的字典获得所需的结果:
{
"Grace Johnson": "26",
"Grace Johnson": "26",
#...
"Elliot McLane": "42",
"Elliot McLane": "42",
}
你需要一个更像这样的结构:
[
{"Name": "Grace Johnson", "Age": "26"},
{"Name": "Grace Johnson", "Age": "26"},
# ....
{"Name": "Elliot McLane", "Age": "42"},
{"Name": "Elliot McLane", "Age": "42"},
]
在任何一种情况下,下一个小问题是字典或适当的字典列表确实需要编写完整的文件,如果您只想随时追加,您可能会查看“json行”数据格式。参见:https://jsonlines.org/
无论如何,考虑到这些限制(并忽略 PrettyTable
,因为它似乎与问题无关)这是一种继续进行的方法。
import json
file_name = "name_dict.txt"
## ---------------------------
## Open our datafile (creating it is needs be)
## ---------------------------
with open(file_name, "a+") as a_file:
a_file.seek(0) # reset the pointer back to the start of the file
test_dict = json.loads(a_file.read() or "{}")
## ---------------------------
while True:
if "quit" == input("Type quit to exit: "):
break
user_name = input("Enter name: ")
user_age = input("Enter age: ")
test_dict[user_name] = user_age
## ---------------------------
## Write the complete dictionary back out
## ---------------------------
with open(file_name, "w") as a_file:
json.dump(test_dict, a_file)
## ---------------------------
for key, value in test_dict.items():
print(f"{key} --> {value}")