简体   繁体   中英

writerows output to a csv file

I am currently printing data from a rest api. It is able to print fine, but I want to save the output to a column on a csv. I get the error

TypeError: writerows() argument must be iterable    

Here is my code:

with open('mycsvfile.csv', 'wb') as f:
    for issue in jira.search_issues('project in (FITQA, UXSCIENCE, '
        'FITSW, FIT) AND status = Resolved AND environment ~ "TC*" '
        'ORDER BY created DESC', maxResults=100):
            a = issue.fields.priority
            print a
            writer = csv.writer(f)
            writer.writerows(a)

writer.writerows() expects an iterable, for example a list, which it can convert into a set of rows. Since this conversion requires an iterable, we're talking about something like a list of lists. writer.writerow() expects an iterable, which it can convert into a row - that is, a list of values. If you want to just write this value as a single column in a one-column csv, you could do

writer.writerow([a])

But if you want it included with other data, you're going to have to produce that list of values:

data.append(a)
writer.writerow(data)

( data here is some existing list of values that you've already assembled)

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