简体   繁体   English

Int Argument必须是字符串或数字,而不是列表,Python csv

[英]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.

相关问题 为什么int()参数必须是字符串或数字,而不是'list'? - Why int() argument must be a string or a number, not 'list'? TypeError:int()参数必须是字符串或数字,而不是“列表” - TypeError: int() argument must be a string or a number, not 'list' int()参数必须是字符串,类似字节的对象或数字,而不是“列出” Python python-tcod Roguelike - int() argument must be a string, a bytes-like object or a number, not 'list' Python python-tcod Roguelike int() 参数必须是字符串或数字 - int() argument must be a string or a number Python 2-TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - Python 2 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' GraphQL 错误:int() 参数必须是字符串或数字,而不是“列表” - GraphQL error: int() argument must be a string or a number, not 'list' 如何在python中将浮点列表转换为int; err int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - how to convert a list of floats to int in python ;err int() argument must be a string, a bytes-like object or a number, not 'list' TypeError:int()参数必须是字符串或数字,而不是使用ouchdb-python的“元组” - TypeError: int() argument must be a string or a number, not 'tuple' with couchdb-python TypeError:int()参数必须是字符串或数字,而不是“ Popen” Python - TypeError: int() argument must be a string or a number, not 'Popen' Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM