我无法让脚本正常工作,我怀疑我忽略了一些简单的事情。我在下面粘贴了一个简化的脚本,它会为类似的数据集生成相同类型的错误。
下面的脚本旨在获取一个 csv,其中每一行代表给定状态的民意调查数据。使用州列表,我想遍历 csv 数据以查找每个州的最新民意调查并生成一个列表列表,该列表总结了该州的一个属性(本例中民主党候选人的投票百分比)。我需要考虑到 csv 文件中的“行”可能没有任何特定顺序并且某些“状态”可能在 csv 中没有数据的可能性。
此示例脚本为第一个州(“阿拉巴马州”)生成正确的输出,但它无法在州列表中找到任何其他州的数据。为什么?
注 1 - 脚本确实获取 csv 文件 注 2 - 如果我不获取 csv 文件,而是将投票数据作为列表列表提供,则脚本按预期工作
谢谢你的帮助。
import csv, httplib2, cStringIO
h = httplib2.Http('.cache')
url = 'http://www.electoral-vote.com/evp2012/Pres/pres_polls.csv'
headers, data = h.request(url)
states = [
"Alabama",
"Alaska",
"Arizona",
"Arkansas",
"California",
"Colorado",
"Connecticut",
"Delaware",
"Florida",
"Georgia",
"Hawaii",
"Idaho",
"Illinois",
"Indiana",
"Iowa",
"Kansas",
"Kentucky",
"Louisiana",
"Maine",
"Maryland",
"Massachusetts",
"Michigan",
"Minnesota",
"Mississippi",
"Missouri",
"Montana",
"Nebraska",
"Nevada",
"New Hampshire",
"New Jersey",
"New Mexico",
"New York",
"North Carolina",
"North Dakota",
"Ohio",
"Oklahoma",
"Oregon",
"Pennsylvania",
"Rhode Island",
"South Carolina",
"South Dakota",
"Tennessee",
"Texas",
"Utah",
"Vermont",
"Virginia",
"Washington",
"West Virginia",
"Wisconsin",
"Wyoming"
]
csv_input = cStringIO.StringIO(data)
csv_output = csv.reader(csv_input)
# sample row =>
#['Day', 'Len', 'State', 'EV', 'Dem', 'GOP', 'Ind', 'Date', '', '', '', '', '', '', '', 'Pollster']
#[' 1.0', '1', 'Wyoming', '3', '33', '65', '', 'Jan 01', '', '', '', '', '', '', '', 'Election 2008-1']
percent_dem_by_state = []
for state in states:
poll_day = 0
percent_dem_for_this_state = [state, None]
for row in csv_output:
if (state == row[2]) and (float(row[0]) > poll_day):
percent_dem_for_this_state = [state, int(row[4])]
poll_day = float(row[0])
percent_dem_by_state.append(percent_dem_for_this_state)
for elem in percent_dem_by_state:
print elem