Here is my code,
for line in open(\'u.item\'):
#read each line
whenever I run this code it gives the following error:
Sometimes when open(filepath)
in which filepath
actually is not a file would get the same error, so firstly make sure the file you're trying to open exists:
import os
assert os.path.isfile(filepath)
hope this will help.
If you are using Python 2
the following will the solution:
import io
for line in io.open("u.item", encoding="ISO-8859-1"):
# do something
Because encoding
parameter doesn't work with open()
, you will be getting the following error:
TypeError: 'encoding' is an invalid keyword argument for this function
As suggested by Mark Ransom, I found the right encoding for that problem. The encoding was "ISO-8859-1"
, so replacing open("u.item", encoding="utf-8")
with open('u.item', encoding = "ISO-8859-1")
will solve the problem.
If someone looking for these, this is an example for converting a CSV file in Python 3:
try:
inputReader = csv.reader(open(argv[1], encoding='ISO-8859-1'), delimiter=',',quotechar='"')
except IOError:
pass
Also worked for me, ISO 8859-1 is going to save a lot, hahaha, mainly if using Speech Recognition API's
Example:
file = open('../Resources/' + filename, 'r', encoding="ISO-8859-1");
Try this to read using pandas
pd.read_csv('u.item', sep='|', names=m_cols , encoding='latin-1')