473,800 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting default output encoding

Hallöchen!

I start a python script with subprocess from another Python script
with

python = subprocess.Pope n(["python", "myscript.p y"],
stdout=subproce ss.PIPE, stderr=subproce ss.PIPE)

However, this child script has sys.stdout.enco ding set to None,
which means that Unicode strings cannot be printed without trouble.
I tried

environment = os.environ
environment["LC_CTYPE"] = environment["LANG"] = "de_DE.UTF-8"
python = subprocess.Pope n(["python", "myscript.p y"],
env = environment,
stdout=subproce ss.PIPE, stderr=subproce ss.PIPE)

for setting it to UTF-8, however, it didn't work. Any idea on how
to achieve this?

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber. org
(See http://ime.webhop.org for ICQ, MSN, etc.)
Aug 6 '07 #1
1 4212
python = subprocess.Pope n(["python", "myscript.p y"],
stdout=subproce ss.PIPE, stderr=subproce ss.PIPE)

However, this child script has sys.stdout.enco ding set to None,
which means that Unicode strings cannot be printed without trouble.
The automatic setting of .encoding is only done when the output is a
terminal. If you write to a file, or a pipe, Python's guess as to what
the encoding of that stream is might be easily wrong.

If you want to be able to write Unicode strings to stdout always,
it's best to wrap stdout with the result of codecs.getwrite r. If
you want to use the user's locale as the encoding, use
locale.getprefe rredencoding().

Regards,
Martin
Aug 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1142
by: nicolas | last post by:
Hello, I hesitated a lot before posting this topic here, but it seems more appropriate than posting it in c.l.perl. I would like to know how to change output encoding when using the perl wrapper for LibXML, i. e. XML::LibXML. I have searched the documentation and the web but cannot find any solution: Whenever I make a DOM access to a document and serialize a node to a string, it
0
1272
by: Cat | last post by:
Hi. First of all, to be exact, it's Visual Web Developer 2005 Express Edition Beta (which can be downloaded from Microsoft freely). When I create a new HTML or ASPX file, the VWD always creates DEFAULT codepage file and saves it with default codepage. Though I can save it with Unicode by selecting "Save as" and clicking "Save with encoding", repeating this every time seems to be a stupid thing. I tried to find an option for this but...
11
5223
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but the "problem" lies in the C library implementation of wcsftime()). I'm not sure if this is a platform-dependent feature or part of the C standard. I've compiled with GCC 3.4.3 on GNU/Linux, and run in an en_GB UTF-8
5
3671
by: Hardy Wang | last post by:
Hi, I have following code: Encoding mode; // Encoding.Default or Encoding.UTF8 FileStream sb = new FileStream(fullPathAndFileName, FileMode.Create); StreamWriter sw = new StreamWriter(sb, mode); sw.Write(textContent); sw.Close(); My question is under what situation, the saved files are different by calling Encoding.Default and Encoding.UTF8.
1
2958
by: jj | last post by:
How do I programatically default the encoding of my "Notepad" into ANSI. My application uses notepad to save some text. Some of the client computers have UTF as the default encoding in their notepad. This adds some characters in my text and creates problems later on managing it. So is there a way to create a text file in notepad with ANSI encoding always? Thanks John
1
2408
by: MaTianyi | last post by:
the encoding of file that is output by the file.write() function is always None. and the encoding attribute of the File Object is readonly. f = open('abc.txt','w') print f.encoding how can I change the file output encoding?
3
11567
by: Chris Shenton | last post by:
I am setting up handlers to log DEBUG and above to a rotating file and ERROR and above to console. But if any of my code calls a logger (e.g., logging.error("foo")) before I setup my handlers, the logging system will create a default logger that *also* emits logs, which I can't seem to get rid of. Is there a way I can suppress the creation of this default logger, or remove it when I 'm setting up my handlers? Thanks. Sample code:
4
2587
by: Ron Garret | last post by:
Is there a way to change the default string encoding used by the string.encode() method? My default environment is utf-8 but I need it to be latin-1 to avoid errors like this: Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 4: ordinal not in range(128) I can't change the code to pass an encoding argument to the decode
0
9551
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,...
0
10507
Oralloy
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...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10036
tracyyun
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...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
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();...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
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
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.