Hello i just started to learn python yesterday and today the guy in the video just started with
print("hello")
Then he executed it without a problem, but when I try to do it, I got the following error message :
SyntaxError: Non-UTF-8 code starting with '\xff' in file
c:/Users/user/desktop/anan_bruh/bune.py on line 1, but no encoding
declared; see http://python.org/dev/peps/pep-0263/ for details
You need to ensure that your file is saved as an "ASCII" format.
If you are using windows, try using a program such as notepad++ or PFE or any other "text editing" program and ensure that the save file type is ASCII, ANSI, Latin or UTF-8.
If you are using Windows notepad, do not save the file as UTF-16. You can set the Encoding when you "Save as...". See the screen shot below:
If you are using Microsoft word or write or some other type of "word processor" - the easiest solution is don't do that! Instead, use notepad, PFE, Notepad++ or any of the hundreds of other "text editing" programs available online.
I hope this helps you.
I just saw the additional comment about using Visual Studio. I do not have that setup, but nevertheless the answer is the same. If I save my little "hello world" script using UTF-16, then I get the exact same error as you.
If I save my file as ANSI or UTF-8, the error is resolved. No doubt there is a way to save the files with the correct encoding within Visual Studio, but maybe try my answer first, verify that the solution works and then try to figure out how to do the same thing in Visual Studio.
Related
I have been opening and rebuilding 2000+ HTML files with python bs4, and the contents of all of them are written in Japanese.
The thing is, I cannot find a proper rule-of-thumb-encoding for those files.
I made try-excepts for the following encodings, but there are still errors.
utf-8, utf-16, utf-32, shift-jis, cp1252, euc
Nothing worked without errors and there were even files that I cannot open at all(but it opens in text editors, though! I tried saving with utf-8, but it never worked too!!!)
I am completely exhausted to find out how can I let my code run without me.
Can anybody help me? What is the error-free encoding to open / write Japanese?
I am making a program involving ANSI escape codes, and they were all working fine on replit until I switched back to IDLE (3.9 on both). But it doesn't work:
it should have looked like this:
I have seen several posts before that complain that the IDLE doesn't support these escape sequences because it isn't a terminal, so I tried to do it directly from the cmd but the beastly symbol still appeared, this time as a boxed question mark:
I know that it won't work straight from the IDLE, so I wonder if you can import a software like mintty into python?
Powershell works though...
P.S. please don't tell me to import colorama or something! I really want this to be the way. I also don't have immediate access to iPython (even though I would like to) so it's not really an option for me... unless I have to :D
EDIT: the code I put across the python programs:
import sys, os
os.system("")
CSI = f"{chr(0x1B)}["
print(f"""{CSI}3m{CSI}1m{CSI}4m{CSI}31m
look at this""")
sys.stdout.flush()
# I put the sys.stdout.flush() and os.system("") to try and fix the problem...
The IDLE shell is not a terminal emulator and not intended to be production environment. The decision so far is that is should show program developers what their program's output, without interpretation. This may change in the future but no final decision yet.
If you are on Windows, I believe its console can be put into ANSI mode, but Python does not do that for you. I don't know if program code can do so.
As near as I can tell, there is not wrapper program that turns mintty into an importable python module. It would not make much sense. Rather, you would want to open mintty or a mintty-based terminal-emulator, such as git bash, and open python within that terminal instead of CommandPrompt.
ANSI code is a broad term. You have to specify which code page you are using. For example, my Windows is in Chinese Simplified. Therefore if I want to escape from UTF-8 default in Python, I would put # coding : cp936 on the first or second line of a script. Then it can read and write text files with the simplified Chinese coding.
Second Question:
Could I make a red/green/etc. font for every character and put it as print('...', file=...)? It should be possible because colored emojis exist.
It should work, but I would like to know how I could (if it's possible) automate this with some program that makes a file containing those characters and displays them with the previous print statement.
Cheers!
So, I'm trying to write a gzip file, actually from the net, but to simplify I wrote some very basic test.
import gzip
LINES = [b'I am a test line' for _ in range(100_000)]
f = gzip.open('./test.text.gz', 'wb')
for line in LINES:
f.write(line)
f.close()
It runs great, and I can see in Jupyter that it has created the test.txt.gz file in the directory listing. So I click on it expecting a whole host of garbage characters indicative of a binary file, like you would see in Notepad.
However, instead I get this ...
Error! test.text.gz is not UTF-8 encoded.
Saving disabled.
See console for more details
Which makes me think, oh my god, coding error, something is wrong with my encoding, my saving, can I save bytes ? Am I using the correct routines ?? And then spend 5 hours trying all combinations of code and modules.
The very simple answer to this is none of the above. This is a very misleading error message, especially when the code you've written was designed to save a binary file with a weird extension.
What this actually means is ...
I HAVE NO IDEA HOW TO DISPLAY THIS DATA ! - Yours Jupyter
So, go to your File Explorer, Finder navigate to the just saved file and open it. Voila !!
Everything worked exactly as planned, there is no error.
Hope this saves other people many hours of debugging, and please Jupyter, change your error message.
It is also possible to select the file and, instead of double-clicking to open, go to 'view' as it interprets it correctly (or well, mostly, depending on special characters, mine is in Spanish and apparently it doesn't support accents).
This way we can avoid looking for the directory where we got the file and not getting out of jupyter :)
When I open a file which has multilanguage character contents arabic contents are not rendering correctly, I setup encoding to utf-8 but did not help. How do you solve this?
On windows the process for getting code to display correctly includes all of:
including a comment on the second line of each source file with #coding:utf-8, the first should really be the shebang #!/usr/bin/env python
ensure that you have a font installed with good Unicode support, including the character page that you are going to be using - Consolas (the default for Wing-IDE) is usually a good choice but AFAIK does not include the full Arabic character ranges however Lucida Console should provide this.
Ensure that Wing-IDE has the Edit->Preferences->User Interface->Fonts->Editor Font/Size set to the selected font, i.e. Licida Console.
I want to paste the output of my Python script into a spreadsheet. For this to work, the output should use hard tabs, and not spaces, to separate fields. How to I convince Python that tabs is what I want?
print("a\tb")
does not do it, and neither does
print("a", "b", sep="\t")
Your help is much appreciated!
Your suggestion of
print("a\tb")
will indeed output a<TAB>b.
Verify this yourself in Python Shell / IDLE, or by writing this a\tb string to file and opening it in any notepad-type program.
The problem likely arises in whatever you're using to view the output. In my current PuTTY setup, tabs are "invisibly" translated to spaces.
EDIT: For xterm, see this "bug" report.