[英]Int Argument Must be a String or a Number, Not a List, Python csv
def load_asn1_data(filename='songdata.csv'):
import csv
reader=csv.reader(open(filename,'r'))
songs=[]
for r in reader:
songs.append(r)
return songs
def fastest_year(songs):
for row in songs:
title = row[0]
artist = row[1]
year = int(row[2])
tempo = float(row[3])
artist_hotness=float(row[4])
duration = float(row[5])
key = int(row[6])
loudness = float(row[7])
mode = int(row[8])
for speed in tempo:
if tempo[speed]==max(tempo):
print year[speed]
songs=load_asn1_data(filename='songdata.csv')
fastest_year(songs)
Update: So, now that I know that the first index refers to rows, I made a for-loop in which each variable corresponds to the value of a particular index in a row. 更新:所以,既然我知道第一个索引引用行,我做了一个for循环,其中每个变量对应一行中特定索引的值。 If I try to run this, then I get a new error, float object is not iterable.
如果我尝试运行它,那么我得到一个新的错误,浮动对象是不可迭代的。 I thought that maybe the max function doesn't work for floats, but I tested it, and it does.
我认为也许max函数不适用于浮点数,但我测试了它,它确实如此。 I'm confused, since I am interpreting my last for-loop as, for all elements in the "tempo class", if the tempo at that index is the largest, print the year associated with that tempo.
我很困惑,因为我正在解释我的最后一个for循环,对于“节奏类”中的所有元素,如果该索引的速度最大,则打印与该速度相关的年份。
if this is the error you are receiving: 如果这是您收到的错误:
TypeError: int() argument must be a string or a number, not 'list'
then your songs[2]
might be like this [2010]
or [xxxx, xxxx]
那么你的
songs[2]
可能就像这[2010]
或[xxxx, xxxx]
please check it by printing either songs[2]
or type(songs[2])
请通过打印
songs[2]
或type(songs[2])
检查它
please make it songs[2][0] 请把它做成歌[2] [0]
year = int(songs[2][0])
songs
is a list of lists, in the fastest_year
function you're actually accessing rows, not columns. songs
是一个列表列表,在fastest_year
函数中,您实际访问的是行,而不是列。 You will need to look through each row in songs
to get the values you want. 您需要查看
songs
每一行以获得所需的值。 Something like: 就像是:
songs=load_asn1_data(filename='songdata.csv')
for song in songs:
fastest_year(song)
Although without knowing exactly what the code is meant to do I can't say if that will work exactly for your purposes. 虽然我不知道代码的确切含义,但我不能说这是否适用于您的目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.