Hello,
I have this little grep-like program:
++++++++++snip+ +++++++++
#!/usr/bin/python
import sys
import re
pattern = sys.argv[1]
inputfile = file(sys.argv[2], 'r')
for line in inputfile:
matches = re.findall(patt ern, line)
if matches:
print matches
++++++++++snip+ +++++++++
Like this, the program prints some characters as strange escape
sequences, which is due to the input file being encoded in utf-8: When I
convert "re.findall ..." to a string and wrap an "unicode()" around it,
the matches get printed correctly. Is it possible to make "matches"
unicode without saving it as a single string first? The function "unicode
()" seems only to work for strings. Or is there a general way of telling
Python to abandon the ancient and evil land of iso-8859 for good and use
utf-8 only?
Regards,
Rehceb 4 2509
Rehceb Rotkiv wrote:
Hello,
I have this little grep-like program:
++++++++++snip+ +++++++++
#!/usr/bin/python
import sys
import re
pattern = sys.argv[1]
inputfile = file(sys.argv[2], 'r')
for line in inputfile:
matches = re.findall(patt ern, line)
if matches:
print matches
++++++++++snip+ +++++++++
Like this, the program prints some characters as strange escape
sequences, which is due to the input file being encoded in utf-8:
So the UTF-8 data gets printed to your terminal which isn't configured
for UTF-8, right?
When I convert "re.findall ..." to a string and wrap an "unicode()" around it,
the matches get printed correctly.
How do you meaningfully convert it to a string? The matches variable
refers to a list, but you surely don't want to be dealing with the
list's string representation.
Is it possible to make "matches" unicode without saving it as a single string first?
Why not convert your input into Unicode and then, for the benefit of
certain kinds of character classes, use re.findall in Unicode mode (by
specifying re.U as a flag)? Then, each match will be produced as a
Unicode object.
The function "unicode()" seems only to work for strings. Or is there a general way of telling
Python to abandon the ancient and evil land of iso-8859 for good and use utf-8 only?
The only refuge from ancient and evil lands is found by climbing the
mountain of Unicode: convert from encoded text as soon as you can,
work only with Unicode objects, produce encoded text only when
necessary.
Paul
Like this, the program prints some characters as strange escape
sequences, which is due to the input file being encoded in utf-8: When I
convert "re.findall ..." to a string and wrap an "unicode()" around it,
the matches get printed correctly. Is it possible to make "matches"
unicode without saving it as a single string first? The function "unicode
()" seems only to work for strings. Or is there a general way of telling
Python to abandon the ancient and evil land of iso-8859 for good and use
utf-8 only?
Python does not live in the ancient and evi land of iso-8859; it lives
in the ancient and evil land of ASCII.
When printing a list, the individual elements are converted with repr(),
not with str(). For a string object, repr() adds escape codes for all
bytes that are not printable ASCII characters. To avoid this call to
repr, you need to iterate over the list yourself, and print it:
if matches:
for m in matches:
print m,
print
HTH,
Martin
Rehceb Rotkiv schrieb:
Hello,
I have this little grep-like program:
++++++++++snip+ +++++++++
#!/usr/bin/python
import sys
import re
pattern = sys.argv[1]
inputfile = file(sys.argv[2], 'r')
for line in inputfile:
matches = re.findall(patt ern, line)
if matches:
print matches
++++++++++snip+ +++++++++
Like this, the program prints some characters as strange escape
sequences, which is due to the input file being encoded in utf-8
As Paul said, your terminal is likely set to iso-8859 encoding, which
is why it doesn't display UTF-8 correctly. The above program produces
correct UTF-8 output.
What you could do is:
1. read the file in as unicode
2. print the unicode to the terminal (will use the terminal encoding) or
convert the unicode to strings with an explicit encoding before printing
codecs.open() is very helpful for step 1, BTW.
Georg
When printing a list, the individual elements are converted with repr(),
not with str(). For a string object, repr() adds escape codes for all
bytes that are not printable ASCII characters.
Thanks Martin, you're right, it were the repr() calls that messed up the
output. Iterating the array like you proposed is even 1/100s faster ;)
Regards,
Rehceb This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Sidney-Woollett |
last post by:
Hi
I need to store accented characters in a postgres (7.4) database, and
access the data (mostly) using the postgres JDBC driver (from a web app).
Does anyone know if:
1) Is there a performance loss using (multibyte) UNICODE vs (single byte)
SQL_ASCII/LATINxxx character encoding? (In terms of extra data, and
searching/sorting speeds).
|
by: Frantic |
last post by:
I'm working on a list of japaneese entities that contain the entity,
the unicode hexadecimal code and the xml/sgml entity used for that
entity. A unicode document is read into the program, then the program
sorts out every doublet and the hexadecimal unicode code is extracted,
but I dont know a way to find the xml or sgml-entity equivalent to the
unicode code. Anyone who could give me a pointer?
Best regards
|
by: Jon Bowlas |
last post by:
Hi listers,
I wrote this script in Zope some time ago and it worked for a while, but now
I'm getting the following error:
TypeError: coercing to Unicode: need string or buffer, NoneType found
Here's my script:
results = context.module_retriever().tuples() # call to ZSQLMethod
converted =
|
by: gabor |
last post by:
hi,
from the documentation (http://docs.python.org/lib/os-file-dir.html) for
os.listdir:
"On Windows NT/2k/XP and Unix, if path is a Unicode object, the result
will be a list of Unicode objects."
i'm on Unix. (linux, ubuntu edgy)
|
by: aine_canby |
last post by:
Hi,
Im totally new to Python so please bare with me.
Data is entered into my program using the folling code -
str = raw_input(command)
words = str.split()
for word in words:
| |
by: Holger Joukl |
last post by:
Hi there,
I consider the behaviour of unicode() inconvenient wrt to conversion of
non-string
arguments.
While you can do:
u'17.3'
you cannot do:
|
by: Gerry |
last post by:
I'm using pyExcelerator and xlrd to read and write data from and to
two spreadsheets.
I created the "read" spreadsheet by importing a text file - and I had
no unicode aspirations.
When I read a cell, it appears to be unicode u'Q1", say.
I can try cleaning it, like this:
|
by: Adam Olsen |
last post by:
As was seen in another thread, there's a great deal of confusion
with regard to surrogates. Most programmers assume Python's unicode
type exposes only complete characters. Even CPython's own functions
do this on occasion. This leads to different behaviour across
platforms and makes it unnecessarily difficult to properly support all
languages.
To solve this I propose Python's unicode type using UTF-16 should have
gaps in its index,...
|
by: Donn Ingle |
last post by:
Hello,
I hope someone can illuminate this situation for me.
Here's the nutshell:
1. On start I call locale.setlocale(locale.LC_ALL,''), the getlocale.
2. If this returns "C" or anything without 'utf8' in it, then things start
to go downhill:
2a. The app assumes unicode objects internally. i.e. Whenever there is
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |