[英]How do I loop through a csv file and create a list based on the values in the list?
I have a csv file with three columns (ID, county, and candidate).我有一个包含三列(ID、县和候选人)的 csv 文件。 There are four candidates and I need to create four lists (one for each candidate).
有四个候选人,我需要创建四个列表(每个候选人一个)。 Every time a candidate's name is in the list/column, I want to add that name to a new list and then get the length of that last to see how many votes they received.
每次候选人的名字出现在列表/列中时,我都想将该名字添加到新列表中,然后获取最后一个列表的长度,以查看他们获得了多少票。 When I run the script, it sucesfully prints the total number of votes cast, but the length of each candidate list is printing "0" so I dont think they are successfully getting added to the list.
当我运行脚本时,它成功打印了投票总数,但是每个候选人列表的长度都打印为“0”,所以我认为他们没有成功添加到列表中。
I am fairly new to python.我对 python 相当陌生。 I believe my error is with how I am looping and my if statements.
我相信我的错误在于我的循环方式和我的 if 语句。
Thank you.谢谢你。
with open(poll_path, 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
#skip the header
next(csvreader,None)
python/
votes = 0
votes = []
candidate_list = 0
candidate_list = []
khan = 'Khan'
khan = []
kahn = 0
correy = "Correy"
correy = []
correy = 0
li = "Li"
li = []
li = 0
otooley = "O'Tooley"
otooley = []
otooley = 0
for row in csvreader:
votes_cast=str(row[0])
votes.append(votes_cast)
candidates=str(row[2])
candidate_list.append(candidates)
if row[2] == str(khan):
khan.append(candidate_list)
if row[2] == str(correy):
correy.append(candidate_list)
if row[2] == str(li):
li.append(candidate_list)
if row[2] == str(otooley):
otooley.append(candidate_list)
total_votes = len(votes)
print("Election Results")
print("----------------------------")
print("Total Votes: " + str(total_votes))
print("----------------------------")
kahn_votes = len(khan)
print(kahn_votes)
correy_votes = len(correy)
print(correy_votes)
li_votes = len(li)
print(li_votes)
otooley_votes = len(otooley)
print(otooley_votes)
You have many problems with your code.你的代码有很多问题。 When you assign a variable multiple times as in
当您多次分配变量时,如
khan = 'Khan'
khan = []
kahn = 0
You keep losing the earlier values.您不断丢失早期的值。
kahn
is 0
and the string and list are gone. kahn
为0
,字符串和列表都消失了。 Those first two lines were pointless.前两行毫无意义。
votes_cast=str(row[0])
The csv
module only creates strings, no need to str
a string. csv
模块只创建字符串,不需要str
。
if row[2] == str(khan):
You know how khan
is 0
?你知道
khan
是0
吗? Yeah, this just compares row 2 to the string "0", so i fails.是的,这只是将第 2 行与字符串“0”进行比较,所以我失败了。 Since
khan
was expected to be string in the first place, you shouldn't have cast it either.由于
khan
一开始就被认为是字符串,所以你也不应该施放它。
There is a much better way to do this using a dictionary to track candidate counts.有一种更好的方法可以使用字典来跟踪候选计数。 General notes on the code
代码的一般说明
open("foo", "r") should be
open("foo") open("foo", "r") should be
open("foo")The code编码
import csv
candidate_count = {}
with open(poll_path, newline=None) as csvfile:
csvreader = csv.reader(csvfile)
for ID, county, candidate in csvreader:
if candidate not in candidate_count:
candidate_count[candidate] = 0
candidate_count[candidate] += 1
# we can sort by total counts to print
for candidate, votes in sorted(candidate_count.items(), key=lambda kv: kv[1]):
print(candidate, votes)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.