0

I am trying to write some output to csv file line by line

Here what I tried:

import csv

today = datetime.datetime.now().date()
filter = "eventTimestamp ge {}".format(today)
select = ",".join([
    "eventTimestamp",
    "eventName",
    "operationName",
    "resourceGroupName",
])

activity_logs = client.activity_logs.list(
    filter=filter,
    select=select
)   

with open(r"C:\scripts\logs.csv", 'w', newline='') as f:
    for log in activity_logs:
        result = (" ".join([
            str(log.event_timestamp),
            str(log.resource_group_name),
            log.event_name.localized_value,
            log.operation_name.localized_value
        ]))
        f.writerow(result)

Its throwing error:

AttributeError: '_io.TextIOWrapper' object has no attribute 'writerow'

How can i fix this error, possibly any other module ?

3
  • 1
    use write and add a \n instead Commented Aug 4, 2020 at 8:03
  • Do you really want to have spaces as separators in your 'CSV' file? Commented Aug 4, 2020 at 8:24
  • no its comma as delimiter Commented Aug 4, 2020 at 8:27

3 Answers 3

1

This:

with open(r"C:\scripts\logs.csv", 'w', newline='') as f:

is creating just text file handle. You need to create csv.writer using f and then you might use writerow, that is:

import csv
...
with open(r"C:\scripts\logs.csv", 'w', newline='') as f:
    writer = csv.writer(f)
    for log in activity_logs:
        result = (str(log.event_timestamp),str(log.resource_group_name),log.event_name.localized_value,log.operation_name.localized_value)
        writer.writerow(result)

You might find useful examples of usage in csv article at PyMOTW-3

Sign up to request clarification or add additional context in comments.

3 Comments

Hi its works now, but how can i add header into this ? I mean for each delimited text, i need to add customised column names in 1st line
headers = ['Column1', 'Column2', 'Column3', 'Column4'] added before for loop, but its not printing as header row
@asp: add writer.writerow(('Column1','Column2','Column3','Column4')) immediately before for loop
1

The error is coming from the line:

f.writerow(result)

and it's telling you that the f object does not have a function named writerow.

As Jannes has commented, use the write function instead:

f.write(result)

Comments

1

CSV.writer is required when your trying to write into CSV . then the code can be

import csv

today = datetime.datetime.now().date()
filter = "eventTimestamp ge {}".format(today)
select = ",".join([
    "eventTimestamp",
    "eventName",
    "operationName",
    "resourceGroupName",
])

activity_logs = client.activity_logs.list(
    filter=filter,
    select=select
)   

with open(r"C:\scripts\logs.csv", 'w', newline='') as file:
    f=csv.writer(file)
    for log in activity_logs:
        result = (str(log.event_timestamp),
            str(log.resource_group_name),
            log.event_name.localized_value,
            log.operation_name.localized_value)
        f.writerow(result)

When the csv.writer is added after opening the csv file it will work without TextIOwrapper error

5 Comments

please add the headers before the for loop starts
some how i am getting , comm after each letter from your code , the solution provided Daweo giving me expected result, but there also i have added headers before for loop, but it does not printing headers, i have used headers = ['Column1', 'Column2', 'Column3', 'Column4']
please add f.writerow(headers) followed by above headers list
its works now :) thank you.. actually stack overflow should allow me to answer multiple answers as accepted where different ppl are helping me , hope they will bring this feature soon :)
ya thats a good idea .You're welcome .please do upvote for the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.