0

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:

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!

ChenBr
  • 1,671
  • 1
  • 7
  • 21
  • 1
    The reader expects a file where the first row contains column names and following rows contain data entries. – Michael Butscher Dec 28 '22 at 10:40
  • @MichaelButscher, I see. Thanks for letting me know. What would be a good approach to ignore the first `n` rows? Also, feel free to add your comment as an answer, and I'll gladly mark it as resolved. – ChenBr Dec 28 '22 at 10:43
  • 1
    There are many questions and answers on this topic: https://stackoverflow.com/questions/48716446/python-skip-header-row-with-csv-reader , https://stackoverflow.com/questions/11349333/how-to-ignore-the-first-line-of-data-when-processing-csv-data – XaC Dec 28 '22 at 10:55
  • @XaC, You're right. Thank you for the links :) – ChenBr Dec 28 '22 at 11:01

2 Answers2

2

Try:

for _ in zip(f, range(5)): 
    pass

before initializing DictReader. This should skip the first 5 lines.

import csv

def read_csv():
    with open("in/2.csv", encoding="utf-8-sig") as f:
        for _ in zip(f, range(5)): 
            pass
        reader = csv.DictReader(f)
        print(list(reader))
read_csv()
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
2

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()
Michael Butscher
  • 10,028
  • 4
  • 24
  • 25