I'm using VSCode Version: 1.46.1 on Mac OS Catalina. I'm using the built-in Python interactive terminal Python 3.7.4 Whenever I print strings, it shows up with unicode, making it difficult to read, like so:
\\u201cI like what we have.\\u201d It is quiet and there is somebody else in the room. I tell my dog that I need to go and he says, \\u201cjust alright.\\u201d ~~I am hungry.\\n\\n
I have tried every flavor of un-escaping escaped characters. See here:
Unescaping escaped characters in a string using Python 3.2
And
Using unicode character u201c
But to no avail. I think the problem lies in the encoding options built into VSCode itself, but I'm not sure how to modify that.
Maybe this page could provide some information for you.
"\u201c" and "\u201d" means “ and ”, but they will not work, they should be "\u201c" and "\u201d".
Related
This question already has answers here:
Python: How can I make the ANSI escape codes to work also in Windows?
(11 answers)
Closed last month.
ANSI code wont work on my python interpreter
I wanted to color some of my prints on the project. I looked up on how to color printed characters and found the ANSI escape codes, so i tried it up on the interpreter, but it wont work.
for example:
print("\033[32m Hello")
it Gives me <-[32m Hello (an arrow left sign).
how do i make it work? can it work on python's interpreter? if not, where should i use it?
Note, this is a possible duplicate to this question answered by this post. A reiteration of the answer by #gary-vernon-grubb is posted below for convenience.
Use os.system('') to ensure that the ANSI escape sequence is processed correctly. An example in the Windows Command Prompt can be seen below:
Ensure that there are no spaces between the ANSI escape sequence and the color code! This was a bit of a pain in the neck for me.
You are best off installing other packages to help generate the ANSI sequences, iirc win32 console does not support ANSI colours natively. One option is to install colorama. The following snippet prints out red text.
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED + 'This is red')
Edit: Upon researching a little more, I've realised that Windows 10 has support for ANSI Escape Sequence and you can do so by calling. Use this if you intend on copy and pasting.
import os
os.system("echo [31mThis is red[0m")
However i'd still prefer the former.
You could be using IDLE... in that case you can't have ANSI colours; the IDLE 'terminal' isn't really a terminal so ANSI codes will show up as a character, whether you type chr(0x1B) or \033 or \x1b; It's all the same.
Your arrow character is normal; I just get a box because I guess the default font doesn't support left arrows...?
But #thatotherguy's explanation might be right... unless you're using IDLE because in that case it's definitely the problem.
Please don't mark my question as already answered, because in all of the questions on stackoverflow or in the Unicode HOWTO I can't figure out how to print the overline or U+203E character in Python 3. Can someone please explain in baby programmer language how to print unicode characters like this one? I have tried some things, but to be honest I had no idea what I was doing.
I am working Kubuntu xenial (16.04).
When I try to print the character I get a UnicodeEncodeError. My question would be, how to work around this error?
EDIT 1: Problem located
I have now figured out my locale is set to POSIX, which would be ASCII encoding. I will try to set it to UTF-8 encoding.
EDIT 2: Still no solution
I have found out what I need to change, I just haven't found out how to. For anyone with the same issue, there's a comment with a link to a post where a similar problem is solved.
EDIT 3: Final answer
Here is a link to an askubuntu forum where I asked how to edit my /etc/default/locale file. Turns out one command in the Linux shell was enough. For me a lot of stuff doesn't work, but this command allowed me to set my locale to en_US.UTF-8: sudo /usr/sbin/update-locale LANG=en_US.UTF-8. After rebooting my OS, the settings had applied and my locale was changed.
Now I don't need the overline character anymore, because I have learned to work with graphics libraries, but I have had multiple problems because of my locale. Thanks to everyone for the advice!
Use \u to indicate a unicode character: print("\u203e").
You need to use the combining character U+0304 instead.
print(u'a\u0304')
ā
U+0305 is probably a better choice (as viraptor suggests). You can also use the Unicode Roman numerals (U+2160 through U+217f) instead of regular uppercase Latin letters, although (at least in my terminal) they don't render as well with the overline.
print(u'\u2163\u0305')
Ⅳ̅
print u'I\u0305V\u0305'
I̅V̅
It seems like your sys.stdout.encoding is 'ascii'.
Try to set it to 'utf-8':
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
print ('\u203e')
print(u' \u0305'*k)
with k as the length of the line in characters.
e.g.
print(u' \u0305'*5)
I've simplified my script so you can focus on the essence my problem.
In AutoKey (not AutoHotKey), I made a Hot-Key (shift-alt-T) that performs this script on any string I have highlighted (like in gedit for example -- but any other gui editor too).
strSelectedText = clipboard.get_selection()
keyboard.send_keys(" " + strSelectedText)
The script modifies the highlighted text and adds a space to the beginning of the string.
It works for most strings I highlight, but not this one:
* Copyright © 2008–2012 Lonnie Best. Licensed under the MIT License.
It works for this string:
* Add a Space 2.0.1
but not on this one:
* Add a Space 2.0.1 –
At the python command prompt, it has no problem any of those strings, yet the clipboard.get_selection() function seems to get corrupted by them.
I'm rather new to python scripting, so I'm not sure if this is an AutoKey bug, or if I'm missing some knowledge I should know about encoding/preparing strings in python.
Please help. I'm doing this on Ubuntu 12.04:
sudo apt-get install autokey-qt
There is no keyboard key for the copyright symbol, or any non-ASCII character like the EN DASH in your third example. Your script code would have to somehow translate any non-ASCII Unicode characters into the appropriate keyboard sequences to generate them. Perhaps you could grab from the clipboard and use a clipboard function to paste the modified string back into your App.
This is a bug in the Python 3 implementation of autokey (special characters not passed through keyboard.send_keys). See GitHub for example.
Newbie disclaimer: I am new to Python and just started using IDLE to play around with Python.
My problem is the interpreter does not recognize strings, whether enclosed in ¨¨ or ´´.
I configured IDLE to use UTF-8. but it does not seem to be an IDLE issue. What I tried is to test this string in the interpreter directly. It does not work.
print ¨Money¨
to the interpereter returns
File "<stdin>", line 1
print ¨Money¨
^ SyntaxError: invalid syntax
Can someone help me with this? I am using a new laptop running fedora15 with the international keyboard layout and python version is 2.7.1.
You need to use "" or '', not ¨¨, ´´ or any other fancy characters.
Just switch your keyboard setting between normal US English for coding, and US English International for texting, that worked for me.
So what I'm trying to do is to join something in the form of
os.path.join('C:\path\to\folder', 'filename').
**edit :
Actual code is :
filename = 'creepy_%s.pcl' % identifier
file = open(os.path.join(self.cache_dir, filename), 'w')
where self.cache_dir is read from a file using configobj (returns string) and in the particular case is '\Documents and Settings\Administrator\creepy\cache'
The first part is returned from a configuration file, using configobj. The second is a concatenation of 2 strings like: 'file%s' % name
When I run the application through the console in windows using the python interpreter installed, I get the expected result which is
C:\\path\\to\\folder\\filename
When I bundle the same application and the python interpreter (same version, 2.6) in an executable in windows and run the app the result is instead
C:\\path\\to\\folderfilename
Any clues as to what might be the problem, or what would cause such inconsistencies in the output ?
Your code is malformed. You need to double those backslashes or use a raw string.
os.path.join('C:\\path\\to\\folder', 'filename').
I don't know why it works in one interpreter and not the other but your code will not be interpreted properly as is. The weird thing is i'd have expected a different output, ie: C:pathtofolder\filename.
It is surprising behavior. There is no reason it should behave in such a way.
Just be be cautious, you can change the line to the following.
os.path.join(r'C:\path\to\folder\', 'filename').
Note the r'' raw string and the final \
Three things you can do:
Use double-slashes in your original string, 'C:\\path\\to\\folder'
Use a raw string, r'C:\path\to\folder'
Use forward-slashes, 'C:/path/to/folder'
I figure it out yesterday. As usual when things seem really strange, the explanation is very simple and most of the times involve you being stupid.
To cut a long story short there were leftovers from some previous installations in dist-packages. The bundled interpreter loaded the module from there , but when i ran the python script from the terminal , the module (newer version) in the current dir was loaded. Hence the "unpredictable" results.