擅长:python、mysql、java
<p>首先,你提供的代码有很多错误。
关键点是,如果在1之前键入2,则应使用变量<strong>my_dict</strong>存储加载的dict或显示空dict</p>
<p>请尝试下面的代码列表:</p>
<pre><code>import csv
def load_data():
open_data = open('file.csv', 'r')
datasets = csv.reader(open_data)
mydict = {row[0]:row[1:] for row in datasets}
return mydict
def display_data(my_dict):
ds = my_dict
if ds == {}:
print("dictionary is empty")
else:
for key, value in ds.items():
print(key)
print(value)
def main():
my_dict = {}
while True:
choice = int(input("select 1 or 2"))
if choice == 1:
my_dict = load_data()
print(my_dict)
elif choice == 2:
display_data(my_dict)
main()
</code></pre>