4

I just left the PC at work (using Python 2.7) and had a script that I was just finishing up (reproduced below). It ran fine at work, I just wanted to add one or two things. But I came home and am using my Mac's version of Python (3.2.2) and I get the following error:

Traceback (most recent call last):
  File "/Users/Downloads/sda/alias.py", line 25, in <module>
    for row_2 in in_csv:
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 304: ordinal not in range(128)

My code is here:

import csv
inname = "Fund_Aliases.csv"
outname = "output.csv"

def first_word(value):
    return value.split(" ", 1)[0]

with open(inname, "r") as infile:
    with open(outname, "w") as out file:
      in_csv = csv.reader(infile)
      out_csv = csv.writer(outfile)

     column_names = next(in_csv)
     out_csv.writerow(column_names)

      id_index = column_names.index("id")
      name_index = column_names.index("name")

      try:
          row_1 = next(in_csv)
          written_row = False

          for row_2 in in_csv:
            if first_word(row_1[name_index]) == first_word(row_2[name_index]) and row_1[id_index] != row_2[id_index]:
                if not written_row:
                    out_csv.writerow(row_1)

                out_csv.writerow(row_2)
                written_row = True
            else:
                written_row = False

            row_1 = row_2
      except StopIteration:
        # No data rows!
        pass
0

1 Answer 1

5

It looks like Fund_Aliases.csv is not an ascii file.

According to the Python3 docs:

Since open() is used to open a CSV file for reading, the file will by default be decoded into unicode using the system default encoding (see locale.getpreferredencoding()). To decode a file using a different encoding, use the encoding argument of open:

with open('some.csv', newline='', encoding='utf-8') as f:
    reader = csv.reader(f)

So try specifying the encoding parameter.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks! That did the trick, I just had to update the following two lines accordingly: with open(inname, "r", encoding = "utf-8") as infile: with open(outname, "w", encoding = "utf-8") as outfile:
Just to clarify, a CSV file on a PC is not the same ASCI format as that CSV file emailed and downloaded onto a Mac. Does this essentially mean that 1-2 lines of code in the Apple operating system makes this file different from that of the Windows operating system?
It could of course make a big difference depending on what is in those 1-2 lines of code. But in general, if there is a difference, it is usually limited to differences in EOL characters ('\n' for unix, versus '\r\n' for Windows), or differences in encoding. Being explicit about the encoding should help avoid that problem.
I sheepishly admit that I'm new to unicode. FYI I found these helpful: docs.python.org/py3k/howto/unicode.html , joelonsoftware.com/articles/Unicode.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.