Python Printing in Terminal - python

I am sorry to post what I think may be a very basic question, but my attempts at solving this have been futile, and I can't find a useful solution that has already been suggested to similar questions on this site.
My basic issue is this: I am attempting to run a file (coding UTF-8) as a program in Mac terminal (running Python 2.7.5). This works fine when I print the results of a mathematical operations, but for some reason I cannot print a simple string of characters.
I have tried running both:
# coding: utf-8
print "Hello, World."
exit()
and
# coding: utf-8
print("Hello, World.")
exit()
Both return an invalid syntax error, with the caret pointing at first set of quotation marks that I've used. What am I missing here?
Thank you for your help!

It turned out that I needed to disable smart quotes in TextEdit.

Related

How to print overline character in python 3?

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)

Alphabetic characters issue

I have to use Turkish characters, when I write "chcp" in cmd, I see 857.
So I tried to start my programs with:
# -*- coding: cp857 -*-
but nothing changed.Still I cant see Turkish characters like "ş,İ,Ş,Ğ" etc.
So I tried to start my programs with:
# -*- coding: cp1254 -*-
with this,I can see the Turkish characters,BUT when my program needs data from user, I cant see the Turkish characters again so my program is nothing. its like:
name=raw_input("Please enter your name: ")
print name
--------
Please enter your name: Ayşe
A*/8e
so,if I have to find user's name in a list in my program, I cant find "Ayşe" because program doesnt understand Turkish characters, but it shows Turkish characters at the beginning of program.The problem appears when I need data from user..
It doesnt make any sense, I really want to know why and how can I fix it.I tried tons of methods,none of them works..
Have a read of How to read Unicode input and compare Unicode strings in Python?, it should help you understand why raw_input isn't reading the name as you expect.
type chcp 65001 in console
right click the cmd window and change fonts to Lucida console
This is a duplicate of Unicode characters in Windows command line - how?
Also, you should really avoid users writing to console, it creates unnecessary complication to both you and the user

Cannot display MySQL latin2_croation_ci characters in Python script

I am trying to apply MySQL records in a Python script. The fields I am concerned with use latin2_croatian_ci collation characters. When I try to print out the following characters,
Karadžić
Stanišić & Simatović
Boškoski & Tarčulovski
Đorđević
Ražnatović, Željko
I get only,
Karadži?
Staniši? & Simatovi?
Boškoski & Tar?ulovski
?or?evi?
Ražnatovi?, Željko
I have tried numerous strategies from both MySQL and Python. In MySQL I have tried both CONVERT and CAST in various combinations. In Python I have tried applying the unicode() function. Nothing seems to work. In Flash, I had to set the embedding to "latin extended A" to solve a similar problem.
Any and all tips and or clues would be appreciated.
Put this line as second line into your python script (after #!/usr/bin/python)
# -*- coding: utf-8 -*-
with utf-8 you should be ok

Repeated errors while working with Portable Python [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
Note: This was not answered by the question that was marked as the original. This is more than just a Python v2 vs v3 problem, which I explain in the comments below.
Original post:
I am trying to learn Python at work, so I am currently using Portable Python 3.2.1.1 (which will henceforth be referred to as PP). (I mention this because this problem doesn't happen at home when I use my Mac and regular Python.)
I am working through exercise 16 of Learning Python the Hard Way (http://learnpythonthehardway.org/book/ex16.html). I've heard this isn't the best learning tool, but I am a complete programming n00b and I'm a hands-on learner. If you have any better suggestions, I'm open!
The first few lines of the exercise read:
from sys import argv
script, filename = argv
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
My script is titled Ex16.py and the file I am using is Python.txt, and both of these are in the same folder as the PP .exes. I don't think that's necessary, but hoped maybe it would fix the problem... negative. When I press "Run" in PP, it doesn't work because argv requires you provide an argument when you start the script: python Ex16.py Python.txt
When I launch Python.exe (which, in PP is Portable-Python.exe), I get the standard Python prompt, >>>, but whatever I enter I get the same error message:
File "<stdin>", line 1
with whatever I've just tried repeated back to me with the marker to
indicate where the problem is. (has not been helpful so far)
SyntaxError: invalid syntax
I have tried typing the following at the >>> prompt:
python Ex16.py Python.txt,,
Ex16.py Python.txt,,
"%PATH&\Ex16.py" "%PATH%\Python.txt" (with the actual filepaths),,
print 'hello world'
I just keep getting the same invalid syntax error over and over. Even a basic print command returned an invalid syntax error. The only one that triggered a different error was the one where I tried whole filepaths. That one returned:
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
position 2-3: truncated \UXXXXXXXX escape
Yes, I have Googled the crap outta both errors. I read that sometimes the problem is not doubling the backspaces, so I tried that, too, putting two \ where just one had been before in both filepaths. I even tried putting — # -*- coding: utf-8 -*- at the beginning of the script thinking maybe there was some unicode error. That, with the full filepaths, resulted in the same unicode error mentioned earlier.
Yes, I have checked that my code is matching that in the exercise.
Yes, this works at home on non-PP.
All this leads me to believe that the problem is probably in the way I'm trying to run the scripts in PP (but why won't print work?), but I haven't a clue what I'm doing wrong.
Thanks!
print is a function in Python 3:
print('my string with content and the like')
It is no longer supported as being a 'statement'. You might want to check out a list of things that changed from python2.x to python3.x (there's a number of incompatibilities). Also, you might be better off finding a tutorial using Python3.
You have to type:
Portable-Python.exe Ex16.py Python.txt
at your command prompt. To get a command prompt, press WindowsKey-R, then type "cmd" and press enter. You should now be looking at something like c:\>. Navigate to your portable python installation by using the cd command.

Help me make my Python 2 code work in Python 3

import math,sys,time;i=0
while 1: sys.stdout.write("\r"+':(_​_)'[:3+int(round(math.sin(​i)))]+'n'+':(__)'[3+int(ro​und(math.sin(i))):]);sys.s​tdout.flush();time.sleep(.​15);i+=0.5*math.pi
I wrote that simple program in Python 2 a long time ago and it worked fine but it has syntax errors in Python 3. I would greatly appreciate if someone could help me update it to be Python 3 compliant. Thanks.
I pasted your code in a file, saved it, then opened it in a Python shell:
In [10]: f=open('test2.py')
In [11]: content=f.read()
In [12]: content
Out[12]: '#!/usr/bin/env python\n# coding: utf-8\n\nimport math,sys,time;i=0\nwhile 1: sys.stdout.write("\\r"+\':(_\xe2\x80\x8b_)\'[:3+int(round(math.sin(\xe2\x80\x8bi)))]+\'n\'+\':(__)\'[3+int(ro\xe2\x80\x8bund(math.sin(i))):]);sys.s\xe2\x80\x8btdout.flush();time.sleep(.\xe2\x80\x8b15);i+=0.5*math.pi\n'
Notice the '\xe2\x80\x8b' bytes sprinkled here and there. These are ZERO WIDTH SPACE characters encoded in utf-8:
In [24]: print(repr(u'\N{ZERO WIDTH SPACE}'.encode('utf-8')))
'\xe2\x80\x8b'
This is why your code is giving rise to SyntaxErrors.
Just retype it (or copy the code below) and it will run in Python3:
import math, sys, time; i=0
while 1: sys.stdout.write('\r'+':(__)'[:3+int(round(math.sin(i)))]+'n'+':(__)'[3+int(round(math.sin(i))):]); sys.stdout.flush(); time.sleep(0.15); i+=0.5*math.pi
The problems has nothing to do with your Python version. You've got weird characters in your code.
I pasted it in Metapad and a bunch of ? showed up, I assume meaning unprintable character.
Just retype it and it will work fine, or find a text editor which will show those characters and delete them, or use Python to delete any non-printable characters.
Indeed, #agf is correct. There was a weird character between the underscores in the first (__). Corrected (and works fine with Python 3):
import math,sys,time;i=0
while 1: sys.stdout.write("\r"+':(__)'[:3+int(round(math.sin(i)))]+'n'+':(__)'[3+int(round(math.sin(i))):]);sys.stdout.flush();time.sleep(.15);i+=0.5*math.pi
Use 2to3 on your python installation. It comes standard (I think) with 2.7.2+

Categories

Resources