gpt4 book ai didi

Python 使用字典、文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:51:13 27 4
gpt4 key购买 nike

<分区>

我有这个代码:

def merge_new_old_urls(urls_list, urls_file_path):
url_dict = {}
try:
with open(urls_file_path, "r") as f:
data = f.readlines()
for line in data:
#read what is already in file
url_dict = { line.split()[0]: int(line.split()[1])}
for new in urls_list:
for key in url_dict.keys():
if new == key:
print 'found'
url_dict[key] += 1
else:
url_dict[new] = 1

except IOError:
logging.critical('no files to read from %s' % urls_file_path)
raise IOError('no files to read from %s' % urls_file_path)
return url_dict

这应该从文件中读取数据并将它与新的数据列表合并,计算它被重复了多少次。具有旧 url 的文件如下所示:

http://aaa.com 1
http://bbb.com 2
http://ccc.com 1

如果新的url列表包含http://aaa.com http://bbb.com字典应该是:

'http://aaa.com':2
'http://bbb.com':3
'http://ccc.com':1

但是我的代码不能正常工作。有人可以治愈吗?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com