0

I'm trying to execute a script on a schedule it's coded in utf-8 at the beginning of the file:

#-*- coding: utf-8 -*- .

I've set Task Scheduler in windows to run a batch file that calls the py file for execution.

python C:/Users/admin_4190248/Desktop/Howard/redshift_howard.py

However, when the script runs in either CMD or PowerShell, it throws a "can't encode error" like this:

UnicodeDecodeError:  'ascii' codec can't encode character u'\u2013'...

Which I don't understand, since the file executes fine in an ipython console.

My question is therefore, is there another (more ipython like) tool that I can use to execute my script on a schedule? Or, can you help to solve this UTF-8 issue within CMD?

Jonathan Kennedy
  • 271
  • 1
  • 2
  • 10
  • That isn't a problem with CMD or Powershell. The error comes from your Python code. You need to properly encode/decode your Unicode characters. – TigerhawkT3 Aug 05 '15 at 19:45
  • Ok, is there any reason this is able to be figured out in ipython, but not in CMD? – Jonathan Kennedy Aug 05 '15 at 19:46
  • Reduce your problem and post the code here. I have a feeling you didn't configure the IO encoding correctly for what you're trying to output. – Ulrich Eckhardt Aug 05 '15 at 19:58
  • convert the text file to ascii. It will throw wing_dings for all your unicode characters it cannot display. Replace those unicode characters in your .py script with the ascii equivalent and ensure it has a proper BOM (byte order mark) signifying it's UTF-8 and you'll likely have a better go once you get to powershell. Otherwise, drop the whole utf-8 format and go for plain text. See the note about Microsoft in this article - https://en.wikipedia.org/wiki/Byte_order_mark – Knuckle-Dragger Aug 05 '15 at 20:00
  • 2
    The problem is the console, not the file. You wouldn't get this error in linux console with proper utf8 support. Windows console is f'ed up, you can change to codepage 65001 (and use a proper) to set it in utf-8 but the last time I tried I still got a plethora of issues. If you search around in StackOverflow there's a lot of post about the subject, with different solutions, but nothing elegant. I'll try to link you something useful soon. I'm not exactly sure how are you using IPython (notebook or console?), maybe you can expand on how you execute in CMD or IPython? – KurzedMetal Aug 05 '15 at 20:10
  • 1
    possible duplicate of [Python, Unicode, and the Windows console](http://stackoverflow.com/questions/5419/python-unicode-and-the-windows-console) – KurzedMetal Aug 05 '15 at 20:26
  • 1
    I'll recommend you looking at [this stackoverflow Q&A](http://stackoverflow.com/a/4637795/236871). Since this subject is a bit complicated and there's enough information about it in this site I'd recommended to close this question as duplicate to avoid spreading good info even more. Read the Q&A I provided previously, there's a lot of information, batch scripts, python code and recommendations to workaround this problem. Good luck, hope it helps – KurzedMetal Aug 05 '15 at 20:28
  • Did you type this out from memory, or is it really a `UnicodeDecodeError` that says it can't *encode* a character? (It helps if you can provide a small script that reproduces the problem. Also include a complete traceback, and please use copy and paste instead of manually copying text.) The default encoding in Python 2 is ASCII. It's never the encoding of the Windows console, so I highly doubt the issue is related to printing to the console -- at least not directly. Also, let's stay on the basic problem without trying to figure out what IPython does. – Eryk Sun Aug 06 '15 at 00:12

0 Answers0