Screen Link:
My Code:
import csv
log = open('example_log.txt')
parsed = parse_log(log)
def build_csv(lines, file, header=None):
if header:
lines = [header] + [l for l in lines]
writer = csv.writer(file, delimiter=',')
writer.writerows(lines)
file.seek(0)
return file
file = open('temporary.csv', 'r+')
csv_file = build_csv(
parsed,
file,
header=[
'ip', 'time_local', 'request_type',
'request_path', 'status', 'bytes_sent',
'http_referrer', 'http_user_agent'
]
)
contents = csv_file.readlines()
print(contents[:5])
What I expected to happen:
I had copy-pasted the given answer as it was throwing errors for me to see the correct answer.
What actually happened:
but the given answer for this task also threw an error
contents is longer than we expected.
The variable contents is a list and it says is longer than expected, the given answer for this task too is not working, please provide the correct solution for this task.