- Please advise if this code can be improved( made smaller) to get the output requested.
LEARN PYTHON: FILES
Writing a CSV File
import csv with open('logger.csv', 'w') as logger_csv: log_writer = csv.DictWriter(logger_csv, fieldnames = fields) log_writer.writeheader() for data in access_log: log_writer.writerow(data) with open('logger.csv') as logger_csv: a = logger_csv.read() print(a)
- How come the fieldnames are defined in the example text, but the code works if I don’t do it. Does that imply that by default dict will take all fields as header and I need to define ’ field ’ variable only when controlling the header values?
When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!
If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer!