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!

4
  • 1
    The reader expects a file where the first row contains column names and following rows contain data entries. Commented Dec 28, 2022 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. Commented Dec 28, 2022 at 10:43
  • 1
    There are many questions and answers on this topic: stackoverflow.com/questions/48716446/… , stackoverflow.com/questions/11349333/… Commented Dec 28, 2022 at 10:55
  • @XaC, You're right. Thank you for the links :) Commented Dec 28, 2022 at 11:01

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.