将编辑后的值更新回原始文件
Updating the edited value back to original file
我有一个文本文件转换成的字典
文本文件:
banana
delicious
yellow
watermelon
big
red
orange
juicy
vitamin c
我要转换的代码是:
file = "C:\Users\user\Downloads\file.txt" #file path
d= {}
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)
d[fruit] = desc
d= {k.upper():v for k,v in d.items()} #capitalise the names
转换成名为d的字典后:
{'BANANA' : ['delicious', 'yellow'],
'WATERMELON' : ['big', 'red'],
'ORANGE' : ['juicy', 'vitamin c']}
我正在努力将附加值添加到键中,不仅是字典本身,还有原始文件。但是我当前的代码删除了所有的键和值,除了我正在添加信息的那个。而且也没有 return 我的钥匙。
def Add_Info():
original_name = input("Please enter the name you would like to modify? ").upper()
additionalinfo = input("Enter information you would like to add? ")
with open(file,'r+') as f:
data = f.read()
data = d[original_name].append(additionalinfo)
f.truncate(0)
f.seek(0)
f.write("\n".join(d[original_name]))
f.close()
for eg, if
original_name = watermelon
additionalinfo = hard,
in the file, it only returns me
big
red
hard
相反,我希望其余的水果、香蕉和橙子保持不变,只需将值 hard 添加到 watermelon[=28= 下的文件中]
以下是实现我在评论中建议的方法的方法。我还进行了一些其他更改以改进代码并遵循 PEP 8 - Style Guide for Python Code 准则:
import itertools
def read_data(file):
d= {}
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)
d[fruit] = desc
d= {k.upper():v for k,v in d.items()} #capitalise the names
return d
def write_data(file, d):
with open(file, 'w') as f:
for key, values in d.items():
f.write(key + '\n')
for value in values:
f.write(value + '\n')
f.write('\n')
def add_info(file):
# original_name = input("Please enter the name you would like to modify? ").upper()
# additional_info = input("Enter information you would like to add? ")
# hardcoded for testing
original_name = "watermelon".upper()
additional_info = "hard"
d = read_data(file)
d.setdefault(original_name, []).append(additional_info)
write_data(file, d)
file = 'fruit_info.txt' #file path
add_info(file)
我有一个文本文件转换成的字典
文本文件:
banana
delicious
yellow
watermelon
big
red
orange
juicy
vitamin c
我要转换的代码是:
file = "C:\Users\user\Downloads\file.txt" #file path
d= {}
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)
d[fruit] = desc
d= {k.upper():v for k,v in d.items()} #capitalise the names
转换成名为d的字典后:
{'BANANA' : ['delicious', 'yellow'],
'WATERMELON' : ['big', 'red'],
'ORANGE' : ['juicy', 'vitamin c']}
我正在努力将附加值添加到键中,不仅是字典本身,还有原始文件。但是我当前的代码删除了所有的键和值,除了我正在添加信息的那个。而且也没有 return 我的钥匙。
def Add_Info():
original_name = input("Please enter the name you would like to modify? ").upper()
additionalinfo = input("Enter information you would like to add? ")
with open(file,'r+') as f:
data = f.read()
data = d[original_name].append(additionalinfo)
f.truncate(0)
f.seek(0)
f.write("\n".join(d[original_name]))
f.close()
for eg, if
original_name = watermelon
additionalinfo = hard,
in the file, it only returns me
big
red
hard
相反,我希望其余的水果、香蕉和橙子保持不变,只需将值 hard 添加到 watermelon[=28= 下的文件中]
以下是实现我在评论中建议的方法的方法。我还进行了一些其他更改以改进代码并遵循 PEP 8 - Style Guide for Python Code 准则:
import itertools
def read_data(file):
d= {}
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)
d[fruit] = desc
d= {k.upper():v for k,v in d.items()} #capitalise the names
return d
def write_data(file, d):
with open(file, 'w') as f:
for key, values in d.items():
f.write(key + '\n')
for value in values:
f.write(value + '\n')
f.write('\n')
def add_info(file):
# original_name = input("Please enter the name you would like to modify? ").upper()
# additional_info = input("Enter information you would like to add? ")
# hardcoded for testing
original_name = "watermelon".upper()
additional_info = "hard"
d = read_data(file)
d.setdefault(original_name, []).append(additional_info)
write_data(file, d)
file = 'fruit_info.txt' #file path
add_info(file)