python openpyxl unicode error - python

I have a problem with writing into xlsx file. I am getting the openpyxl.utils.exceptions.IllegalCharacterError error. On the top of my code there is written:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Also the line that raises the error is:
ws[name_line] = z.text.encode('utf-8').strip()
So now I really don`t know what to do.

The IllegalCharacterError Exception raises when you want to assing special character to a Cell.
You don't have to encode Strings by yourself, openpyxl always encodes to utf-8.
Change your code, for instance to:
ws['A1'] = z.text.strip()
Come back and Flag your Question as answered if this is working for you or comment why not.

Related

Python: Unable to write slanted apostrophe to file

I'm using Python 2.7 and unable to upgrade to 3.x just yet.
I need to read data from the database and write to a file.
Using the database query tool, I see the string I need to retrieve contains the following slanted apostrophe:
I’ve
When I read the database from python and simply print it to the console, I see the following. It has converted the slanted apostrophe to a normal apostrophe. This is actually my preferred behavior:
print(message)
I've
But when I try to write the string to a file, it instead writes the apostrophe as a question mark:
I?ve
My original code just does this:
file = open(path,"w")
file.write(message)
file.close()
To try to fix it, I did the following, but it did not help. The question mark is still showing up:
# -*- coding: utf-8 -*-
import codecs
file = codecs.open(path,"w", "utf-8")
file.write(message)
file.close()

Python Notepad++ encoding error

I am using Python for the first time and am running into an encoding error that I can't seem to get around. Here is the code:
#!/usr/bin/python
#-*- coding: utf -*-
import pandas as pd
a = "C:\Users"
print(a)
When I do this, I get:
File "C:\Users\Public\Documents\Python Scripts\ImportExcel.py", line
5
a = "C:\Users"
^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in positio n 2-3: truncated \UXXXXXXXX escape
In Notepad++ I have tried all of the encoding options. Nothing seems to work.
Any suggestions?
Specifically, the problem is that the '\' is an escape character.
If you want to print the string
"C:\Users"
then you have to do it thus:
a = "C:\\Users"
Hope this helps.
The error message suggests you're on a Windows machine, but you're using *nix notation for #!/usr/bin/python. That line should look something like #!C:\Python33\python.exe on a Windows machine, depending on where you've installed Python.
Use this: # -*- coding: utf-8 -*- instead of #-- coding: utf --
You can set the encoding in Notepad++, but you also need to tell Python about it.
In legacy Python (2.7), source code is ASCII unless specified otherwise. In Python 3, source code is UTF-8 unless otherwise specified.
You should use the following as the first or second line of the file to specify the encoding of the source code. The documentation gives:
# -*- coding: <encoding> -*-
This is the format originally from the Emacs editor, but according to PEP263 you can also use:
# vim: set fileencoding=<encoding>:
of even:
# coding=<encoding>
Where <encoding> can be any encoding that Python supports, but utf-8 is generally a good choice for portable code.

emacs constantly asking before saving python code with # -*- coding: ASCII -*-

I have a python script that starts with:
#!/usr/bin/env python
# -*- coding: ASCII -*-
and prior to saving, it always splits my window, and asks:
Warning (mule): Invalid coding system `ASCII' is specified
for the current buffer/file by the :coding tag.
It is highly recommended to fix it before writing to a file.
and I need to say yes, it there a way to disable this ? Sorry for asking but I had no luck on google.
Gabriel
A solution that doesn't involve changing the script is to tell Emacs what ASCII means as a coding system. (By default, Emacs calls it US-ASCII instead.) Add this to your .emacs file:
(define-coding-system-alias 'ascii 'us-ascii)
Then Emacs should be able to understand # -*- coding: ASCII -*-.
The Python Enhancement Proposal (PEP) 263, Defining Python Source Code Encodings, discusses a number of ways of defining the source code encoding. Two particular points are relevant here:
Without encoding comment, Python's parser will assume ASCII
So you don't need this at all in your file. Still, if you do want to be explicit about the file encoding:
To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file, such as:
# coding=<encoding name>
(note that the = can be replaced by a :). So you can use # coding: ascii instead of the more verbose # -*- coding: ASCII -*-, as suggested by this answer. This seems to keep emacs happy.

A source file with unicode characters is making Django throw up a SyntaxError exception

A file in UTF-8 encoding has an è character (e with accent grave) embedded in comment delimiters for Python. Django complains about this character and will not render the page. How can I resolve this?
The SyntaxError Django is raising already points you in the right direction.
It is always a good thing to actually read exceptions. In your case, it will have said something along the lines of
Non-ASCII character '\xc3' in file /home/zakx/../views.py on line 84, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 84)
If you'd then read PEP-0263, you could learn that there are some ways to tell Python (and your editor!) which encoding your files are in. Generally, you will want to use UTF-8 encoding whenever possible. Therefore, writing one of the following lines into the first line (or second, if you use a shebang) will tell Python to use UTF-8 for that file.
# coding=utf8
# -*- coding: utf8 -*-
# vim: set fileencoding=utf8 :
Did you try adding the coding header to the file? On the first line, possibly after the shebang line, add
# -*- coding: utf-8 -*-

How to generate hebrew strings in python 3?

I'm trying to create hebrew strings but get syntax errors. It works in the IDLE shell but not in Pydev.
Here's what I've tried so far:
s = 'מחרוזת בעברית' #works in the shell only
s = u'מחרוזת בעברית' #doesn't work at all
s = unicode("מחרוזת בעברית", "UTF-8") #also doesn't work at all
I get a syntax error: Non-UTF-8 code starting with '\xee'.
What does it mean and what shall I do to create hebrew strings?
Does your source file start with a # -*- coding: utf-8 -*- line? Is your file actually encoded as utf-8 (and not some other encoding)?
It's supposed to work (the first line, other lines are not valid Python 3).

Categories