Error when trying to open file - python-3.x

I've created a txt file by Microsoft Word and saved it as texta.txt in a folder untitled. I've also saved a python file as texti.py in the same folder untitled.
When going back to Wing IDE, i typed the following:
infile = open('texta.txt')
lines = infile.readlines()
and it returned:
Traceback (most recent call last):
File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 2, in <module>
if __name__ == '__main__':
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
builtins.UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5 in position 192: ordinal not in range(128)
i want to open the file (text), and print it into the python shell.

When you save a file as .txt, you should specify the encoding instead of letting the editor choose for you, and not tell you. LibreOffice has two text SaveAs options: 'Text (.txt)' and 'Text - choose encoding (.txt)'. MS Word should also have an option to select the encoding. If not, get an editor that does. And select UTF8 unless you have a reason to select otherwise.
The particular error message suggests to me that your text file is not utf8. A likely alternative is 'latin1'. If so, b'\xd5'.decode('latin1') == 'Õ'. If 'Õ' is the 195th char in your file, use encoding=latin1. If not, you could guess something else. Or reopen with MS Word on the same machine and save with a known encoding.

Related

ascii codec error is showing while running python3 code via apche wsgi

Objective: Insert a Japanese text to a .ini file
Steps:
Python version used 3.6 and used Flask framework
The library used for writing config file is Configparser
Issue:
When I try running the code via the "flask run" command, there are no issues. The Japanese text is inserted to ini file correctly
But when I try running the same code via apache(wsgi) I am getting the following error
'ascii' codec can't encode characters in position 17-23: ordinal not in range(128)
Never interact with text files without explicitly specifying the encoding.
Sadly, even Python's official documentation neglects to obey this simple rule.
import configparser
config_path = 'your_file.ini'
config = configparser.ConfigParser()
with open(config_path, encoding='utf8') as fp:
config.read_file(fp)
with open(config_path, 'w', encoding='utf8') as fp:
config.write(fp)
utf8 is a reasonable choice for storing Unicode characters, pick a different encoding if you have a preference.
Japanese characters consume up to five bytes per character in UTF-8, picking utf16 (always two bytes per character) can result in smaller ini files, but there is no functional difference.

Non-UTF-8 code starting with '\x85' in file firstday.py on line 4, but no encoding declared

The following code is generating the error shown in the title, please can someone explain what it means, what is causing it and how to fix?
year = eval(input("Enter the year: "))
day = (1+5*((year-1)%4)+4*((year-1)%100)+6*((year-1)%400))%7
print("The 1st of January",year,"falls on day",day,end = " ")
print("of the week(0=Sun,…,6=Sat).")
You have saved the Python source code file in a different encoding than UTF-8, namely Windows-1252, because you are working on Windows.
Switch your text editor to UTF-8 and run the program again.
Python 3 expects its source code to be saved as UTF-8 unless a different encoding is declared. The … is the offending character (character code 85 in Windows-1252). This character code is invalid in UTF-8, so Python fails to read the source code and gives the error you see.
When you re-save the file as UTF-8, the Unicode code point HORIZONTAL ELLIPSIS (U+2026) will be used, which becomes 3 bytes in the file (E2, 80, A6), and Python will be happy.
Alternatively you can explicitly declare your source code to be Windows-1252, this is equally valid and will work as well.

How to open a text file from my desktop while using python 3.7.1 in Terminal

I saved a text file to my desktop named "test.txt" within the file I wrote only my name, David. Then, I opened terminal and opened python 3.7.1 and wrote the following code in attempt to see my name, David, populate:
open("/Users/David/Desktop/test.txt,"r")
However, I receive the following error message:
SyntaxError: EOL while scanning string literal
Does anyone know how I can avoid this error and have my name, David, read from the test.txt file on my desktop? Or am I going about this completely wrong?
As #Matt explained, you are missing quotes.
You can follow below approach to open file and read from it.
myfile = open("/Users/David/Desktop/test.txt","r") #returns file handle
myfile.read() # reading from the file
myfile.close() # closing the file handle, to release the resources.
For more information on how to do read/write operations on file
You are missing a quotation mark, after your file path. It should look like this:
open("/Users/David/Desktop/test.txt","r")
^ This quotation mark
This will open the file correctly, however you will still need to actually read from it.
You are missing the other quotations as the others have mentioned. Try using the with open statement, as it handles your resources for you, meaning you don't need to specify .close()
with open("/Users/David/Desktop/test.txt", "r") as file:
file.read()
you can use with which will close the file automatically as you come out of the block and put your Directory link
with open(r"Directory_link", "r") as file1:
FileContent = file1.read()
print(FileContent)

both read and modify a .txt file in python

How can i both read and modify a .txt file in python?
I am trying to write a programme using which i modify a text file using given user input. But if i use 'r' within open, it only reads.
I have tried:
sanskrit = open("sanskrit.txt","r")
sans = sanskrit.read()
print(sans)
sanskrit.close
Error i got:
Traceback (most recent call last):
File "/home/pi/Desktop/AnuragEK/coding/try.py/", line 3,in
sans = sanskrit.read()
Can anyone help me?
Thanks in advance.
If you are trying to both read and write from/to a file, you should use the mode 'r+' instead of 'r'. There are a few other modes for doing this but 'r+' is probably the one you want.

How to convert Linux Python 3.4 code with national characters into executable code in windows

My national language is Polish.
I've got program in Python 3.4 which I wrote on linux. This program mostly work on text, Polish text. So of course, variable names don't have any special characters, but sometimes I put into them some strings with Polish characters, user will input from keyboard some strings with Polish characters and My program read from files, where I got strings with Polish characters.
Everything work well on Linux. I didn't think about encoding, it just worked. But now i want to make it work on Windows. Can you help me understand, what I should actually do to make this transform?
Or maybe some workaround - I just need to have Windows executable file. Perfect way for this, would be "Pyinstaller", but it work only for python 2.7, not 3.4. That's why I want to make it working on Windows, and in VirtualBox with py2exe compile into executable form. But maybe somone know way for this in Linux, it without this encoding problems, it would be great.
If not, I back to my question. I tried to convert my python scripts in gedit into ISO or CP1250 or 1252, I wrote in the file headline what coding I'm using, it actually worked a little, now my windows error pint me into my files with text form which I read some data, so I converted them too... But it didn't work.
So I decided, that it's no more time for blind trials, and I need to ask for help, I need to understand what encoding is used on windows, which on linux, what is the best way to convert one into another, and how make program read characters in right way.
The best way would be - I guess - not changing anything in encoding, but just make windows python understand what encoding I'm using. Is that possible?
Complete answer for my question would be great, but anything what will point me in right direction will also help me a lot.
OK. I'm not sure, if I understand your answer in comments, but tried sending text for myself via mail, coping it in virtualbox into notepad and save as utf_8. Still get this message:
C:\Users\python\Documents>py pytania.py
Traceback (most recent call last):
File "pytania.py", line 864, in <module>
start_probny()
File "pytania.py", line 850, in start_probny
utworzenie_danych()
File "pytania.py", line 740, in utworzenie_danych
utworzenie_pytania_piwo('a')
File "pytania.py", line 367, in utworzenie_pytania_piwo
for line in f: # Czytam po jednej linii
File "C:\Python34\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1134: cha
racter maps to <undefined>
As mentioned by Zero Piraeus in a comment: The default source encoding for Python 3.x is UTF-8, regardless of what platform it's running on...
If you have problems, that probably because your source code has incorrect encoding. You should stick to UTF-8 only (even though PEP 0263 -- Defining Python Source Code Encodings allows changing it).
The error message you provided is clear:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1134
Python is currently expecting UTF8 (because "UnicodeDecodeError"!), but it encounters an illegal char (0x9d isn't a valid char is UTF8). To diagnose the problem, use iconv(1) on a Linux machine, to detect errors buy doing a dummy conversion:
iconv -f utf8 -t iso8859-2 -o /dev/null < test.py
You can try to reproduce the problem by creating a very simple python file, typically : print "test €uro encoding"

Resources