473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

sys.stdin.encoding

The following line in my code is failing because sys.stdin.encoding is
Null. This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?

uni=unicode(word,sys.stdin.encoding)

Thanks,

Aine.

Dec 11 '06 #1
8 5042
ai********@yahoo.com wrote:
The following line in my code is failing because sys.stdin.encoding is
Null.
I'll guess you mean None rather than Null.
This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?

uni=unicode(word,sys.stdin.encoding)
You could give it a fallback value:

uni = unicode(word, sys.stdin.encoding or sys.getdefaultencoding())

or even just:

uni = unicode(word, sys.stdin.encoding or 'ascii')

which should be the same in all reasonable universes (although I did get
bitten recently when someone had changed the default encoding in a system).
Dec 11 '06 #2
Duncan Booth skrev:
ai********@yahoo.com wrote:
The following line in my code is failing because sys.stdin.encoding is
Null.

I'll guess you mean None rather than Null.
This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?

uni=unicode(word,sys.stdin.encoding)
You could give it a fallback value:

uni = unicode(word, sys.stdin.encoding or sys.getdefaultencoding())

or even just:

uni = unicode(word, sys.stdin.encoding or 'ascii')

which should be the same in all reasonable universes (although I did get
bitten recently when someone had changed the default encoding in a system).

Thanks for your help. The problem now is that I cant enter the Swedish
characters åöä etc without getting the following error -

Enter wordPåe
Traceback (most recent call last):
File "C:\Documents and Settings\workspace\simple\src\main.py", line
25, in <module>
archive.Test()
File "C:\Documents and Settings\workspace\simple\src\verb.py", line
192, in Test
uni=unicode(word,sys.stdin.encoding or sys.getdefaultencoding())
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1:
ordinal not in range(128)

The call to sys.getdefaultencoding() returns ascii. Since I can enter
the characters åöä on the command line in Pydef/Eclipse doesn't that
mean that the stdin is not ascii? What should I do?

Thanks again,

Aine.

Dec 11 '06 #3
ai********@yahoo.com schrieb:
The following line in my code is failing because sys.stdin.encoding is
Null. This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?

uni=unicode(word,sys.stdin.encoding)
That's a problem with pydev, where the standard machinery to determine
the terminal's encoding fail.

I have no idea yet how to fix this.

Regards,
Martin
Dec 11 '06 #4
ai********@yahoo.com wrote:
The call to sys.getdefaultencoding() returns ascii. Since I can enter
the characters åöä on the command line in Pydef/Eclipse doesn't that
mean that the stdin is not ascii? What should I do?
I think that depends on what sort of script you are writing.

If it is just something for personal use on a single machine, or if you
know that the same encoding is going to be used on all systems where you
have this problem, then you could hardwire the encoding to whatever it
should be (maybe 'latin1').

If it is to be used across a variety of systems with different encodings
then you'll have to figure out some way to find the correct encoding.
Dec 11 '06 #5

Duncan Booth skrev:
ai********@yahoo.com wrote:
The call to sys.getdefaultencoding() returns ascii. Since I can enter
the characters åöä on the command line in Pydef/Eclipse doesn't that
mean that the stdin is not ascii? What should I do?
I think that depends on what sort of script you are writing.

If it is just something for personal use on a single machine, or if you
know that the same encoding is going to be used on all systems where you
have this problem, then you could hardwire the encoding to whatever it
should be (maybe 'latin1').

If it is to be used across a variety of systems with different encodings
then you'll have to figure out some way to find the correct encoding.
Yeah I'm only learning python so I think I can go with latin1.

Cheers,

Aine.

Dec 11 '06 #6

ai********@yahoo.com wrote:
Duncan Booth skrev:
ai********@yahoo.com wrote:
The following line in my code is failing because sys.stdin.encoding is
Null.
I'll guess you mean None rather than Null.
This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?
>
uni=unicode(word,sys.stdin.encoding)
>
You could give it a fallback value:

uni = unicode(word, sys.stdin.encoding or sys.getdefaultencoding())

or even just:

uni = unicode(word, sys.stdin.encoding or 'ascii')

which should be the same in all reasonable universes (although I did get
bitten recently when someone had changed the default encoding in a system).


Thanks for your help. The problem now is that I cant enter the Swedish
characters åöä etc without getting the following error -

Enter wordPåe
Traceback (most recent call last):
File "C:\Documents and Settings\workspace\simple\src\main.py", line
25, in <module>
archive.Test()
File "C:\Documents and Settings\workspace\simple\src\verb.py", line
192, in Test
uni=unicode(word,sys.stdin.encoding or sys.getdefaultencoding())
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1:
ordinal not in range(128)

The call to sys.getdefaultencoding() returns ascii. Since I can enter
the characters åöä on the command line in Pydef/Eclipse doesn't that
mean that the stdin is not ascii? What should I do?
The workaround in your case is:

in the beginning of your program:

import sys
if hasattr(sys.stdin, 'encoding'):
console_encoding = sys.stdin.encoding
else:
import locale
locale_name, console_encoding = locale.getdefaultlocale()

and later:

uni = unicode(word, console_encoding)

But don't think it's portable, if you use other IDE or OS, it may not
work. It would be better if PyDev implemented sys.stdin.encoding

-- Leo

Dec 11 '06 #7

Martin v. Löwis wrote:
ai********@yahoo.com schrieb:
The following line in my code is failing because sys.stdin.encoding is
Null. This has only started happening since I started working with
Pydef in Eclipse SDK. Any ideas?

uni=unicode(word,sys.stdin.encoding)

That's a problem with pydev, where the standard machinery to determine
the terminal's encoding fail.

I have no idea yet how to fix this.
Environmental variable TERMENCODING ? Heck, maybe this will catch on
and will be used by other languages, libraries, terminals, etc. It's
not really Python only problem.

-- Leo

Dec 11 '06 #8
Leo Kislov schrieb:
Environmental variable TERMENCODING ? Heck, maybe this will catch on
and will be used by other languages, libraries, terminals, etc. It's
not really Python only problem.
I also considered environment variables. This can likely be made
available only in 2.6, though.

Plus, in the Eclipse case, we can't know for sure that stdout
really goes to the terminal - as it is just a pipe file descriptor
(Java is not capable of managing a full pseudo-terminal).

Regards,
Martin
Dec 11 '06 #9

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

Similar topics

9
by: It's me | last post by:
Why do I get an "AttributeError: read" message when I do: import sys r=sys.stdin.read() ?? I've tried: r=sys.stdin.read(80)
6
by: ccdrbrg | last post by:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin capability using the dlopen() API. Sequencing program commands (typed) and...
2
by: velle | last post by:
My headache is growing while playing arround with unicode in Python, please help this novice. I have chosen to divide my problem into a few questions. Python 2.3.4 (#1, Feb 2 2005, 12:11:53) ...
8
by: comp.lang.php | last post by:
<? error_reporting(E_ALL & ~E_NOTICE); if (@is_file('functions.inc.php')) require_once('functions.inc.php'); $xml = preg_replace('/(>)+(<)/', '$1$2', @file_get_contents('php://stdin')); $parser...
1
by: BenjaMinster | last post by:
I want to read and write unicode on stdin and stdout. I can't seem to find any way to force sys.stdin.encoding and sys.stdout.encoding to be utf-8, so I've got the following workaround: import...
1
by: yc | last post by:
I have a encoding problem during using of subprocess. The input is a string with UTF-8 encoding. the code is: tokenize =...
5
by: Antimon | last post by:
Hi, I'm new to c/c++ and working on string stuff with visual studio 2005. I'm trying to understand something, for example when i do this: wstring st; wcin >st; if the input is pure ascii,...
7
by: Markus Mayer | last post by:
Hi folks. I'm somewhat new to *nix programming and just ran into a problem. I have to take user input from the terminal but like to constrain that to some rules given, i.e. "numbers only" or...
5
by: dave_140390 | last post by:
Hi, I have problems getting my Python code to work with UTF-8 encoding when reading from stdin / writing to stdout. Say I have a file, utf8_input, that contains a single character, é, coded...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.