简体   繁体   中英

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. 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. 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.

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]

please check it by printing either songs[2] or type(songs[2])

please make it songs[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. You will need to look through each row in songs to get the values you want. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM