csv.Error: iterable expected trying to save a CSV file from a For

everything good? I need some help to save this script in CSV that reads a CSV and transforms the data through a lib. I've been racking my brain for hours and I can't figure out why I can't save the CSV file. Can anybody help me? I am a beginner in python and I am learning the tool to use in ETL processes.

import csv
from user_agents import parse

with open('UserAgent.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)
    idUser = 0
    space = ' / '
    for line in csv_reader:
        user_agent = parse(line[0])
        idUser = idUser + 1
        with open('data.csv', 'w') as f:
            writer = csv.writer(f)
            writer.writerow(user_agent)

Upvotes: 0

Views: 139

Answers (1)

haja
haja

Reputation: 88

writer.writerow expects an iterable. Your user_agent must not be an iterable.

Try

writer.writerow( [user_agent] )

instead of

writer.writerow(user_agent)

Check if that's what you want.

Upvotes: 1

Related Questions