Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading .csv file with Python and DictReader missing columns

I have the following .csv file I’m trying to read as a whole:

---------------------------------------------------------------
#INFO            |           |        |       |        |      |
---------------------------------------------------------------
#Study name      | g1        |        |       |        |      |
---------------------------------------------------------------
#Respondent Name | name      |        |       |        |      |
---------------------------------------------------------------
#Respondent Age  | 20        |        |       |        |      |
---------------------------------------------------------------
#DATA            |           |        |       |        |      |
---------------------------------------------------------------
Row              | Timestamp | Source | Event | Sample | Anger|
---------------------------------------------------------------
1                | 133       | Face   | 1     | 3      | 0.44 |
---------------------------------------------------------------
2                | 240       | Face   | 1     | 4      | 0.20 |
---------------------------------------------------------------
3                | 12        | Face   | 1     | 5      | 0.13 |
---------------------------------------------------------------
4                | 133       | Face   | 1     | 6      | 0.75 |
---------------------------------------------------------------
5                | 87        | Face   | 1     | 7      | 0.25 |
---------------------------------------------------------------

Image of the file

This is the code I am using to open, read the file, and print to the terminal:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import csv

def read_csv():
    with open("in/2.csv", encoding="utf-8-sig") as f:
        reader = csv.DictReader(f)
        print(list(reader))
read_csv()

The output I am getting includes only the first two columns of my .csv:

[{'#INFO': '#Study name', '': ''}, {'#INFO': '#Respondent Name', '': ''}, {'#INFO': '#Respondent Age', '': ''}, {'#INFO': '#DATA', '': ''}, {'#INFO': 'Row', '': 'Anger'}, {'#INFO': '1', '': '4.40E-01'}, {'#INFO': '2', '': '2.00E-01'}, {'#INFO': '3', '': '1.30E-01'}, {'#INFO': '4', '': '7.50E-01'}, {'#INFO': '5', '': '2.50E-01'}]

Why are the other columns missing from my output? I want the other columns starting from Row to be included as well. Any direction would be appreciated. Thanks!

>Solution :

The DictReader expects a file where the first row contains column names (each name different from each other) and following rows contain data entries. If multiple column names are just empty strings, the value in the last column is assigned to key '' in the returned dictionary.

To skip the first five lines instead, you can write:

import csv

def read_csv():
    with open("in/2.csv", encoding="utf-8-sig") as f:
        for _ in range(5):
            next(f)

        reader = csv.DictReader(f)
        print(list(reader))
read_csv()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading