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 |
---------------------------------------------------------------
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!
CodePudding user response:
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()
CodePudding user response:
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()
CodePudding user response:
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()