kdialog和unicode [英] kdialog and unicode

查看:123
本文介绍了kdialog和unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力让python,unicode和kdialog很好地一起玩。

这是一台linux机器,kdialog是一种在
$ b中生成对话框的方法用户可以与之交互的$ b kde(例如输入文本),你可以在你的脚本中使用输出的文本。


无论如何,我是什么我正在使用

编解码器模块读取utf-8编码的文本文件,并使用以下内容:


data = codecs.open( ''file'',''r'',''utf-8'')


然后我操纵数据将其分解为文本片段。


然后我运行这个命令:

test = os.popen('''kdialog --inputbox%s''%(data))



Traceback(最近一次调用最后一次):

文件" ;< stdin>",第1行,在?

UnicodeEncodeError:'' ascii''编解码器无法编码字符u''\ u017a''

位置272:序数不在范围内(128)


我真的很喜欢kdialog将文本显示为utf-8。但是,看起来好像是在python尝试将utf-8编码数据传递为ascii,这显然会因为无法处理utf-8而失败。编码文本。是否可以将文本作为utf-8传递给kdialog,而不是ascii?


或者我完全误解了整个过程,其中案件,可以

你请赐教。


马特

解决方案

Dumbkiwi写道:

我正在尝试让python,unicode和kdialog很好地一起玩。
这是一个linux机器,kdialog是一种生成对话框的方法用户可以与之交互的kde中的框(例如输入文本),您可以在脚本中使用输出的文本。

无论如何,我在做什么正在使用
编解码器模块读取utf-8编码的文本文件,并使用以下内容:

data = codecs.open(''file'',''r'', ''utf-8'')


数据现在是一个unicode字符串。

然后我操纵数据将其分解为文本片段。 />
然后我运行它命令:

test = os.popen(''kdialog --inputbox%s''%(data))


Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
UnicodeEncodeError:''ascii''编解码器不能编码字符你在位置272:ordinal不在范围内(128)

我真的希望kdialog将文本显示为utf-8。然而,似乎python试图将utf-8编码的数据传递为ascii,这显然会因为无法处理utf-8编码的文本而失败。是否可以将文本作为utf-8而不是ascii传递给kdialog?




只需在传递目标编码之前对其进行编码to os.popen():


test = os.popen(''kdialog --inputbox%s''%data.encode(" utf-8"))


Peter


2005年4月26日星期二11:41:01 +0200,Peter Otten写道:

Dumbkiwi写道:

我正在尝试让python,unicode和kdialog很好地一起玩。
这是一个linux机器和kdialog是一种在kde中生成用户可以与之交互的对话框(例如输入文本)的方法,并且您可以在脚本中使用输出的文本。

data = codecs.open(''档案'',''r'',''utf-8 ')



数据现在是一个unicode字符串。

然后我操纵数据将其分解为文本片段。

然后我运行这个命令:

> test = os.popen(''kdialog --inputbox%s''%(data))


Traceback(最近一次调用最后一次):
文件"< stdin>" ,第1行,在?
UnicodeEncodeError:''ascii''编解码器无法在
位置272编码字符u''\ u017a'':序数不在范围内(128)

我非常希望kdialog将文本显示为utf-8。但是,似乎python试图将utf-8编码的数据传递为ascii,
这显然会失败,因为它无法处理utf-8编码的文本。
是有可能将文本作为utf-8传递给kdialog,而不是ascii吗?



只需将数据编码为目标编码,然后再传递给
os.popen():

test = os.popen(''kdialog --inputbox%s''%data.encode(" utf-8"))

>彼得




我曾尝试过,但随后文字看起来像废话。我正在使用

的文本是波兰文,而且

中有很多非英文字符。使用这种方法会产生一些奇怪的字符 - 基本上它

看起来像是用utf-8编码的文件,但使用iso-8859-1显示。


这是我能做的最好的吗?


感谢您的帮助。


Matt


Dumbkiwi写道:

只需将数据编码为目标编码,然后再传递给
os.popen():

test = os.popen(''kdialog --inputbox%s''%data.encode(" utf-8"))


我曾尝试过,但随后文字看起来像垃圾。我正在使用
的文字是波兰文,那里有很多非英文字符。使用这种方法会产生一些奇怪的字符 - 基本上它看起来像是用utf-8编码的文件,但使用iso-8859-1显示。

这是我能做的最好的吗?




我刚刚在我的Suse 9.1上尝试了你描述的设置(使用德语变音符号而不是

波兰语字符),它

Python 2.3和2.4可以正常工作。也许您需要的目标编码不是UTF-8。我想b $ b会尝试其他流行的编码用于波兰语文本(不知道这些是什么...... / b $ b)。 sys.stdout.encoding可能会给你一个线索。


彼得


I''m trying to get python, unicode and kdialog to play nicely together.
This is a linux machine, and kdialog is a way to generate dialog boxes in
kde with which users can interact (for example input text), and you can
use the outputted text in your script.

Anyway, what I''m doing is reading from a utf-8 encoded text file using the
codecs module, and using the following:

data = codecs.open(''file'', ''r'', ''utf-8'')

I then manipulate the data to break it down into text snippets.

Then I run this command:

test = os.popen(''kdialog --inputbox %s'' %(data))


Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: ''ascii'' codec can''t encode character u''\u017a'' in
position 272: ordinal not in range(128)

I would really like kdialog display the text as utf-8. However, it seems
that python is trying to pass the utf-8 encoded data as ascii, which
obviously fails because it can''t deal with the utf-8 encoded text. Is it
possible to pass the text out to kdialog as utf-8, rather than ascii?

Or have I completely misunderstood the whole process, in which case, can
you please enlighten me.

Matt

解决方案

Dumbkiwi wrote:

I''m trying to get python, unicode and kdialog to play nicely together.
This is a linux machine, and kdialog is a way to generate dialog boxes in
kde with which users can interact (for example input text), and you can
use the outputted text in your script.

Anyway, what I''m doing is reading from a utf-8 encoded text file using the
codecs module, and using the following:

data = codecs.open(''file'', ''r'', ''utf-8'')
data is now a unicode string.

I then manipulate the data to break it down into text snippets.

Then I run this command:

test = os.popen(''kdialog --inputbox %s'' %(data))


Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: ''ascii'' codec can''t encode character u''\u017a'' in
position 272: ordinal not in range(128)

I would really like kdialog display the text as utf-8. However, it seems
that python is trying to pass the utf-8 encoded data as ascii, which
obviously fails because it can''t deal with the utf-8 encoded text. Is it
possible to pass the text out to kdialog as utf-8, rather than ascii?



Just encode the data in the target encoding before passing it to os.popen():

test = os.popen(''kdialog --inputbox %s'' % data.encode("utf-8"))

Peter


On Tue, 26 Apr 2005 11:41:01 +0200, Peter Otten wrote:

Dumbkiwi wrote:

I''m trying to get python, unicode and kdialog to play nicely together.
This is a linux machine, and kdialog is a way to generate dialog boxes
in kde with which users can interact (for example input text), and you
can use the outputted text in your script.

Anyway, what I''m doing is reading from a utf-8 encoded text file using
the codecs module, and using the following:

data = codecs.open(''file'', ''r'', ''utf-8'')



data is now a unicode string.

I then manipulate the data to break it down into text snippets.

Then I run this command:

> test = os.popen(''kdialog --inputbox %s'' %(data))


Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: ''ascii'' codec can''t encode character u''\u017a'' in
position 272: ordinal not in range(128)

I would really like kdialog display the text as utf-8. However, it
seems that python is trying to pass the utf-8 encoded data as ascii,
which obviously fails because it can''t deal with the utf-8 encoded text.
Is it possible to pass the text out to kdialog as utf-8, rather than
ascii?



Just encode the data in the target encoding before passing it to
os.popen():

test = os.popen(''kdialog --inputbox %s'' % data.encode("utf-8"))

Peter



I had tried that, but then the text looks like crap. The text I''m using
for this is Polish, and there are a lot of non-English characters in
there. Using this method results in some strange characters - basically it
looks like a file encoded in utf-8, but displayed using iso-8859-1.

Is this the best I can do?

Thanks for your help.

Matt


Dumbkiwi wrote:

Just encode the data in the target encoding before passing it to
os.popen():

test = os.popen(''kdialog --inputbox %s'' % data.encode("utf-8"))

I had tried that, but then the text looks like crap. The text I''m using
for this is Polish, and there are a lot of non-English characters in
there. Using this method results in some strange characters - basically it
looks like a file encoded in utf-8, but displayed using iso-8859-1.

Is this the best I can do?



I''ve just tried the setup you described (with German umlauts instead of
Polish characters) on my Suse 9.1, and it works as expected with both
Python 2.3 and 2.4. Perhaps the target encoding you need is not UTF-8. I
would try other popular encodings used for Polish text (no idea what these
are). sys.stdout.encoding might give you a clue.

Peter


这篇关于kdialog和unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆