-1

The below problem is temporarily fixed. I figured out that the input file (csv) has special characters (e.g. Aimí©) that resulted in the error. I now manually change the characters (e.g. Aimí© --> Aime).

Previous question:

I am using unicodecsv while writing to a csv file. My program ran smoothly until I encountered the below error. Could someone help?

Code:

import unicodecsv as csv
output_csv_write = open(csv_file_name_write,'w')
csv_file_write = csv.writer(output_csv_write, encoding='utf-8')
csv_file_write.writerow([An array of info])

First error:

UnicodeDecodeError: 'utf8' codec can't decode bytes in position 3-4: unexpected end of data

After seeing the above error, I try to debug by running:

    csv_file_write.writerow(['A','B','C'])

and I received the second error as below:

     84     def writerow(self, row):
     85         return self.writer.writerow(
---> 86                 _stringify_list(row, self.encoding, self.encoding_errors))
     87 
     88     def writerows(self, rows):

ValueError: I/O operation on closed file
Tommy N
  • 365
  • 1
  • 4
  • 12
  • I don't get that error in Python 2.7.6, unicodecsv 0.14.1. – Robᵩ Jun 15 '16 at 15:57
  • @Robᵩ My apology. I just realized that it happened after I encountered another problem. I am modifying my question to provide more details. – Tommy N Jun 15 '16 at 16:25
  • 2
    Please provide the shortest **complete** program that demonstrates the error you are having. Since the error is data-dependent, please include a short sample set that causes the error. See [mcve] for more info. – Robᵩ Jun 15 '16 at 16:30
  • According to the Python 2 [csv docs](https://docs.python.org/2/library/csv.html) files handled by the `csv` module should be opened in binary mode, (on platforms where that makes a difference) and that also applies to `unicodecsv`, since it's a drop-in replacement. – PM 2Ring Jun 15 '16 at 16:49
  • @Robᵩ Thank you so much for your help! It took me a while to figure out what went wrong with my code. I realized that the input file (csv) has special characters (e.g. Aimí©) that resulted in the error. Temporarily, I am manually changing those characters (e.g. Aimí© --> Aime) to avoid the problem. – Tommy N Jun 15 '16 at 19:16
  • It sounds like your original file was not UTF-8 encoded. You didn't need to change the data to fix your problem – Alastair McCormack Jun 17 '16 at 10:02
  • @AlastairMcCormack Could you help advise how I could do it without changing the data? I am very new to web scraping and programming, and I would very appreciate your advice. – Tommy N Jun 17 '16 at 15:50
  • @TommyN you need to provide all your code, the source file (preferably a link to the original so that the encoding is as-is), and the full stack trace of your error – Alastair McCormack Jun 17 '16 at 15:51

1 Answers1

1

Try the following:

import unicodecsv as csv

with open(csv_file_name_write, "wb") as f:
    writer = csv.writer(f, encoding="utf-8")
    writer.writerow(["A", "B", "C"])

If that does not solve your problem, unfortunately, I cannot help you because this is working on my machine.

Simon Kirsten
  • 2,542
  • 18
  • 21