Skip to content

An error on chapter 7.2.1 #16

@Terrygmx

Description

@Terrygmx

The code reading csv data and using list comprehension in this chapter gets an error in my Windows:
"Error: iterator should return strings, not bytes (did you open the file in
text mode?)"
BTW, the code is:
`from csv import DictReader
data_rdr = DictReader(open(r'F:\Learn\python data wrangling\data-wrangling-master\data\unicef\mn.csv','rb'))
header_rdr = DictReader(open(r'F:\Learn\python data wrangling\data-wrangling-master\data\unicef\mn_headers.csv','rb'))

data_rows = [d for d in data_rdr]
header_rows = [h for h in header_rdr]

print(data_rows[:5])
print(header_rows[:5])`
I searched in stackoverflow, which says

You need to wrap the file in a io.TextIOWrapper() instance, and you need to figure out the encoding

Is this correct?
I am using Python 3.5.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions