jackiekazil / data-wrangling

Code repository for Data Wrangling with Python (O'Reilly)
559 stars 564 forks source link

An error on chapter 7.2.1 #16

Open Terrygmx opened 6 years ago

Terrygmx commented 6 years ago

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

jjing11 commented 5 years ago

I had the same problem. The file is not a binary file. You can try this: from csv import DictReader with open("C:/Users/Administrator/Desktop/data_wrangling/mn.csv", "rt", encoding="utf-8") as mnfile: data_rdr = DictReader(mnfile) data_rows = [d for d in data_rdr] print(data_rows[:5]) with open("C:/Users/Administrator/Desktop/data_wrangling/mn_headers.csv", "rt", encoding="utf-8") as mn_headersfile: header_rdr = DictReader(mn_headersfile) header_rows = [h for h in header_rdr] print(header_rows[:5])

Terrygmx commented 5 years ago

Thank you! I will give it a try!

jackiekazil commented 5 years ago

@jjing11 Thank you for chiming in! <3

padonu commented 5 years ago

Had the same problem, but your @jjing11 solution was helpful Thank you!