python如何修改字典的值,修改字典python中的值

I have a text file-turned-dictionary using itertools.

import itertools

plantfile = 'myplants.txt' #file path

plant = {} #create an empty dictionary

with open(plantfile, 'r') as f:

for empty_line, group in itertools.groupby(f, lambda x: x == '\n'):

if empty_line:

continue

fruit, *desc = map(str.strip, group)

plant[fruit] = desc

The result is:

{'banana' : ['delicious', 'yellow'],

'watermelon' : ['big', 'red'],

'orange' : ['juicy', 'vitamin c']}

I would like to prompt the user the name of the fruit (key) to modify both the key and description (value) or delete the whole record. It would be the best if I can use def () function.

The following are my current codes. They do not return me any error messages, however, they are also not reflected back in the dictionary file.

print("What do you want to do with this database?

1. Modify fruit information

2. Delete fruit record")

choice == input("Please enter your choice in number: ")

while True:

try:

if choice == '1':

original_name = input("What is the fruit name you would like to modify?").upper()

new_name = input("What is the new fruit name?")

with open(file, 'r+') as f:

string = f.read()

string = string.replace(original_name, new_name)

f.truncate(0)

f.seek(0)

f.write(string) #writeback to file

print(f"You have modified {original_name} to {new_name}.")

break

else:

delname = input("Please enter the name of the fruit: \n").upper()

delname = dict(filter(lambda item: delname in item[0], plant.items()))

break

except ValueError:

print("Invalid input. Please try again.")

I can't figure out how to modify the values and it seems like the above code to modify the name of the key isn't working either.

The code above to delete the whole record is also not being reflected in the original file.

for eg, I would like to change the value of watermelon from 'big' to 'hard' and wishes to delete the whole record of banana.

{'watermelon' : ['hard', 'red'],

'orange' : ['juicy', 'vitamin c']}

Please help me out. Thank you

解决方案

This solution is for the question before the edit. There was an .upper() in the original_name input taken previously. The modified code is as follows:

import itertools

file = 'file.txt'

plant = {}

with open(file, 'r') as f:

for empty_line, group in itertools.groupby(f, lambda x: x == '\n'):

if empty_line:

continue

fruit, *desc = map(str.strip, group)

plant[fruit] = desc

print(plant)

original_name = input("What is the fruit name you would like to modify?")

new_name = input("What is the new fruit name?")

with open(file, 'r+') as f:

string = f.read()

string = string.replace(original_name, new_name)

f.seek(0)

f.write(string) #writeback to file

print(f"You have modified {original_name} to {new_name}.")

Assuming your file.txt looks like this:

banana

delicious, yellow

watermelon

big, red

orange

juicy, vitamin cc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值