Python CSV files - Is there a better way to read and write a file than what i've done

Hi

  1. Please advise if this code can be improved( made smaller) to get the output requested.

LEARN PYTHON: FILES

Writing a CSV File

https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-files/exercises/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)
  1. 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?

Thank you

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! :slight_smile:

Does this code work? No errors? What did you want to improve. Improve is very broad?
Can you elaborate what you mean by field name

2 Likes

Hi
code works. wanted to know if i could have read write functionality for csv file in one instance, instead of opening it in read and write mode separately.

thanks

Can you show what you mean? I can’t duplicate it if you are talking about calling

log_writer = csv.DictWriter(logger_csv)   #, fieldnames = fields)

# The above results in:
Traceback (most recent call last):
  File "script.py", line 6, in <module>
    log_writer = csv.DictWriter(logger_csv)   #, fieldnames=fields)
TypeError: __init__() missing 1 required positional argument: 'fieldnames'

fieldnames is a required parameter for DictWriter.

Writing a CSV file

Hi . this is the exercise I did. Output is fine, but my understanding of this is not. Need serious help there.

As you can see, i used two lines of code to read and write to the file. I wonder if i could read a file in its write mode to reduce the code lines?
And second , I do not understand the fieldnames functionality yet. What is its role?

Kindly help.

I’m not sure that “reducing code lines” is a worthy primary goal (although I am in favor of simplification, not necessarily the same thing.) Can you describe how that would work, if you could do it? Would the reading take place before, after or during the writing?


As for fieldnames, from the docs: DictWriter() “… maps dictionaries onto output rows. The fieldnames parameter is a sequence of keys that identify the order in which values in the dictionary passed to the writerow() method are written to file f .”

Looking at the example from the exercise:

big_list = [{'name': 'Fredrick Stein', 'userid': 6712359021, 'is_admin': False},
            {'name': 'Wiltmore Denis', 'userid': 2525942, 'is_admin': False},            
             {'name': 'Greely Plonk', 'userid': 15890235, 'is_admin': False},
             {'name': 'Dendris Stulo', 'userid': 572189563, 'is_admin': True}] 

import csv

with open('output.csv', 'w') as output_csv:
    fields = ['name', 'userid', 'is_admin']
    output_writer = csv.DictWriter(output_csv, fieldnames=fields)

    output_writer.writeheader()
    for item in big_list:
        output_writer.writerow(item)

Look at the input file. It is a list of dictionaries, all having the same keys. The object is to output a table having the keys as a header, and the values as rows:

Run the code. Look at the output file It takes a bit of extra work to get the spacing, but this is basically what you see:

name,             userid,      is_admin
Fredrick Stein,   6712359021,  False
Wiltmore Denis,   2525942,     False
... etc.

Now, sa that, you’d like a different order. Just change the fields assignment to:
fields = ['userid', 'is_admin', 'name']
… and you get:

userid,       is_admin,    name
6712359021,   False,       Fredrick Stein
2525942,      False,       Wiltmore Denis
... etc.  
2 Likes

Can you describe how that would work, if you could do it?

I tried reading output_csv using this code

with open('output.csv', 'w') as output_csv:
    fields = ['name', 'userid', 'is_admin']
    output_writer = csv.DictWriter(output_csv, fieldnames=fields)

    output_writer.writeheader()
    for item in big_list:
        output_writer.writerow(item)
    output_read = output_csv.read()
    print(output_read)

Gave me this error = io.UnsupportedOperation: not readable
So i guess that answers my first query.

Thank you for explaining fieldnames. Its much clear now.

1 Like

:ok_hand: thank you for the above explanation

2 Likes