我有一个任务要做,我被卡住了,因为我所做的一切似乎都不起作用。
所以我必须要csv文件。 首先调用people_file,它包含标题行:id,名称,姓氏。 以及包含id,person_id,网站的visits_file。 我必须编写一个称为merge的函数,该函数以文件作为参数(均为StrionIO类型)获取文件,并返回字典列表以及每个用户的访问次数:
[ {
"id": (person's id),
"name": (person's name),
"surname": (person's surname),
"visits": (number of visits)
} ]
我想到了这个,但我不知道我的错误在哪里。
import io
def merge(persons_file,visits_file):
line_counter = 0
return_list = []
list_of_person_ids = []
visits = 0
for row in visits_file:
if line_counter == 0:
line_counter+=1
continue
list_of_person_ids.append(row.split(',')[1])
line_counter = 0
for row in persons_file:
if line_counter == 0:
line_counter+=1
continue
help_dict = {}
split_row = row.split(',')
help_dict['id'] = split_row[0]
help_dict['name'] = split_row[1]
help_dict['surname'] = split_row[2][:len(split_row[2])-1]
if split_row[0] in list_of_person_ids:
visits = list_of_person_ids.count(split_row[0])
help_dict['visits'] = str(visits)
return_list.append(help_dict)
visits=0
return return_list
file1 = open('persons_file.csv' , mode='r')
file2 = open('visits_file.csv' , mode='r')
persons_file_arg = io.StringIO(file1.read())
visits_file_arg = io.StringIO(file2.read())
list_of_visits = merge(persons_file_arg,visits_file_arg)
for i in list_of_visits:
print(i)
file1.close()
file2.close()
如果有人能帮助我,我会很高兴。
答案 0 :(得分:0)
出了什么问题?是不是您所期望的输出,还是您遇到异常?您的代码似乎应该可以达到所需的结果,但是我有几点建议可以简化事情。
collections.Counter
,然后可以调用count_of_visits_by_person_id = Counter(list_of_person_ids)
以获取以下形式的结果:
{person_id: number_of_visits, ...}
。然后,您可以使用它简单地在下一个for循环中查找访问次数。例如:from collections import Counter
...
count_of_visits_by_person_id = Counter(list_of_person_ids)
for row in persons_file:
if line_counter == 0:
line_counter += 1
continue
help_dict = {}
split_row = row.split(',')
help_dict['id'] = split_row[0]
help_dict['name'] = split_row[1]
help_dict['surname'] = split_row[2][:-1]
# [:len(split_row[2]) - 1] is equivalent to [:-1]
# I assume you are stripping whitespace from the right side,
# which can also be accomplished using split_row[2].rstrip()
if split_row[0] in count_of_visits_by_person_id:
visits = count_of_visits_by_person_id[split_row[0]]
else:
visits = 0
help_dict['visits'] = str(visits)
return_list.append(help_dict)
with
语句。这是一个示例:with open('visits_file.csv', mode='r') as visits_file:
row = visits_file.readline()
while row:
row = visits_file.readline() # Skips the first line
list_of_person_ids.append(row.split(',')[1])