Python Forum
Error while loading the csv file
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error while loading the csv file
#1
Hi , While I am trying to load csv file using the below code :
***********************************************************
# Load a CSV file
def load_csv(filename):
file = open(filename, "rb")
lines = reader(file)
dataset = list(lines)
return dataset

# load and prepare data
filename = 'D:/python/Training/Decision Tree/data.csv'
dataset = load_csv(filename)
***********************************************************
Error Traceback (most recent call last)
<ipython-input-7-798d54f914f1> in <module>()
11 # load and prepare data
12 filename = 'data_banknote_authentication.csv'
---> 13 dataset = load_csv(filename)
14
15

<ipython-input-7-798d54f914f1> in load_csv(filename)
6 file = open(filename, "rb")
7 lines = reader(file)
----> 8 dataset = list(lines)
9 return dataset
10

Error: iterator should return strings, not bytes (did you open the file in text mode?)

Can anyone please suggest , why this is happening ?
Reply
#2
You are opening the file in binary mode - ' rb '. Open it just for reading - file = open(filename, "r")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
The csv module is iterating over the file object. It relies on the fact, that a iterating over a file, which has been opened in text mode, yields one line per iteration step.

Open the file implicit in text mode with open(filename, "r") or open(filename) or do it explicit with open(filename, "rt")

Further information about open and it's modes: https://docs.python.org/3.6/library/functions.html#open
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Thanks to all for helping me out . It worked .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Next/Prev file without loading all filenames WilliamKappler 9 458 Apr-12-2024, 05:13 AM
Last Post: Pedroski55
  Error loading Trelby.py blackclover 3 2,512 Jan-05-2021, 10:08 PM
Last Post: blackclover
  Loading large .csv file with pandas hangejj 2 2,375 Jun-08-2020, 01:32 AM
Last Post: hangejj
  Importing/loading a library file (.so) tomasby 0 2,071 Aug-24-2019, 08:13 PM
Last Post: tomasby
  Error loading location on openweathermap via API with Kivy Gigux 10 5,549 Apr-24-2019, 06:08 PM
Last Post: Yoriz
  Help loading INT from a file. lovepeace 1 1,887 Apr-13-2019, 12:55 PM
Last Post: ichabod801
  Getting error while loading excel(.xlsx) file using openpyxl module shubhamjainj 1 8,964 Mar-01-2019, 01:05 PM
Last Post: buran
  error creating new object after loading pickled objects from file arogers 2 3,418 Feb-02-2019, 10:43 AM
Last Post: Larz60+
  PyQt5 No Error But GUI Window Not Loading digitalmatic7 1 4,227 Nov-21-2017, 05:23 PM
Last Post: digitalmatic7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020