(I'm using Python 3.4 for this, on Windows)
So, I have this code I whipped out to better show my troubles:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
os.startfile('C:\\téxt.txt')
On IDLE it works as it should (it just opens that file I specified), but on Console (double-click) it keeps saying Windows can't find the file. Of course, if I try to open "text.txt" instead it works perfectly, as long as it exists.
It's slowly driving me insane. Someone help me, please.
You are using the wrong encoding, try using cp1252 -
# -*- coding: cp1252 -*-
Your file name is 'C:\téxt.txt',try to use 'C:\text.txt'
Related
I'm running Python 3.5.2 and am trying to do some basic stuff with unicode and UTF-8. I'm currently just trying to output non-ASCII characters and am unable to do so. For example, this:
ddd = '\u0144'
print(ddd)
gives me a Unicode encode error, telling me that the character maps to undefined. From what I understand of unicode in Python 3.5.2, mapping should happen automatically. I tried putting # -*- coding: utf-8 -*- before the code and various combinations of .decode and .encode as well, but to no avail.
PM 2Ring, typing in chcp 65001 in command prompt did the trick. Thanks!
I'm trying to run my kivy app using autorun on my Raspberry Pi.
After restarting my OS it will run but during execution I faced up with encoding problem on the next lines of code:
CommonData.deviceSettings.Measurement.Alchogol = {}
for s in alchogolSettings:
key = s["Name"].encode('utf-8').strip()
value = s["Value"].encode('utf-8').strip()
CommonData.deviceSettings.Measurement.Alchogol.update({key: value})
The next error occured during execution
'ascii' codec can't encode characters in position 0-14: ordinal not in
range(128)
In the top of my .py file I setup next instructions:
#!/usr/bin/python
# -*- coding: utf8 -*-
The most interesting in this situation that if I will run this program from usual terminal it launch successfully, but when I'm trying to launch it using autorun this problem occurs
Does anybody know why this problem occurs and how to avoid it?
I found the reason of this problem. In my case I run a python script which is launch terminal and start another python script. The last script print some cyrrilic text to the terminal. This is where the problem lies. After deleting this print instruction I avoided this error. And this print instruction was located one line after the code I showed in this post
Problem while importing module
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import win32com.client
when I run it in eclipse, syntaxError occurs.
but it runs perfectly on Windows console.
how to type the right coding of pywin32?
For eclipse unicode console support:
Add -Dfile.encoding=UTF-8 to eclipse.ini which is in the eclipse install directory.
In eclipse – Run\Run Configurations\Python Run\configuration\Common\make sure UTF-8 is selected
In eclipse – Window\Preferences\General\Workspace\Text file encoding\making sure UTF-8 is selected
In [python install path]\Lib\site.py – chane from encoding = “ascii” to encoding = “utf-8”
Make sure you’re using unicode supporting fonts in eclipse – Window\Preferences\Appearance\Colors and Fonts\Debug\Console font\Edit
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?
I have a Python script which works perfectly in Eclipse Console (Run configuration).
When I try to launch this script on a Windows 7 console, I have the encoding error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc9' in position 0: ordinal not in range(128)
I changde the code page of my Windows console to use the same one as in Eclipse (Window->Perference->General->Worspace->Text file encoding):
chcp 1252
At the beginning of the script, I add:
# -*- coding: cp1252 -*-
But it changes nothing.
It works on Eclipse console, so I do not want to decode/encode all my strings for Windows console.
Have you any idea or advice to fix that behaviour?
You could try setting both eclipse's and the windows cmd line's encodings to Utf-8 and see if that works, unless you absolutely need the cp1252 encoding.
The issue is that Python will expect your 8 bit strings to contain ASCII only, not Unicode. u'\xc9 is a Unicode character. Perhaps Eclipse is more friendly than Windows 7 console. You should use the unicode command to convert characters to Unicode as you get them:
value = unicode(value, "utf-8")
See this article for more.