0

Completely new to Python here... I hope this is not a stupid question.

Tried following the posts like the following:

  1. Is 'encoding is an invalid keyword' error inevitable in python 2.x?
  2. Ansi to UTF-8 using python causing error

But couldn't really solve my problem.

So what I have here is a block of code in Python 3 format:

with open(fresh_file,'r', encoding='latin-1') as targ_f:
new_data_iter = csv.reader(targ_f,
                       delimiter = ',',
                       quotechar = '"')
new_data = [new_data for new_data in new_data_data_iter]

I have Python 2, so I want to change this code so that it becomes compatible with Python 2.

Here's what I've tried so far reading above posts:

with open(fresh_file,'r') as targ_f:
new_data_iter = csv.reader(targ_f.decode('latin-1'),
                       delimiter = ',',
                       quotechar = '"')
new_data = [new_data for new_data in new_data_data_iter]

but then I get an error code:

AttributeError: 'file' object has no attribute 'decode'

So I read upon the decode() documentation, it can only be done on string objects, and since my target_f is a file object, it cannot be applied. but then the new_data_iter is a cvs.reader type obj, so also cannot be applied. Now, I am not sure how to incorporate decode() into the python 2 code...

Community
  • 1
  • 1
alwaysaskingquestions
  • 1,595
  • 5
  • 22
  • 49

0 Answers0