Unicode问题-HELP [英] A Unicode problem -HELP

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

问题描述

我正在编写一个程序,将ascii字母列表翻译成需要unicode编码的另一种语言。这就是我到目前为止所做的



1.我有???# - * - 编码:UTF-8 - * - 作为我的第一行。

2.在Wing IDE中,我将默认编码设置为UTF-8

3.我已导入编解码器并打开并写入我的文件,这不是'' t $ / $
有一个BOM,编码= UTF-8

4.我写了一本字典进行翻译,其中的条目如

{ ''F'':u''\ u0254''}以及进行翻译的功能


一切正常,除了我的输出文件,当加载到

unicode意识到的emeditor已经

(u''F'',u''\ u0254'')


但是我想要显示为:

(''''',''é?'')#哪里有é?是一个回到前面'''''


所以我的问题是:

1.我该怎么做?

2.我是否需要更改上述任何步骤?

解决方案

manstey写道:

1.我有# - * - 编码:UTF-8 - * - 作为我的第一行。
2.在Wing IDE中我将默认编码设置为UTF-8
3.我已导入编解码器并打开和写入我的文件,它没有BOM,编码= UTF-8
4.我写了一本字典进行翻译,其中包含
{等词条''F'':u''\ u0254''}和翻译功能

一切正常,除了我的输出文件,加载到
unicode识别emeditor已经
(u''F'',u''\ u0254'')


我不能完全遵循这样的描述:什么是你的输出文件

(它创建的步骤是什么?),以及


(u' F,u''\\\ɔ’ )


进入这个文件?什么是

产生该输出线的精确Python语句?

所以我的问题是:
1.我该怎么做?




最有可能的是,您使用(直接或间接)repr()函数

将元组转换为该字符串。你不应该这样做;

相反,你应该自己格式化元组的元素,例如

通过


print>> f,u"(''%s'',''%s'')" %价值


问候,

Martin


嗨Martin,

正如我写的那样:


input_file = open(input_file_loc,''r'')

output_file = open(output_file_loc,' 'w'')
对于input_file中的行


output_file.write(str(word_info + parse + gloss))#= three

返回元组的函数


(u''F'',u''\ u0254'')是返回的众多unicode元组元素中的两个

由三个功能组成。


我做错了什么?


" manstey" <毫安***** @ csu.edu.au>写道:

input_file = open(input_file_loc,''r'')
output_file = open(output_file_loc,''w'')
for input_file :
output_file.write(str(word_info + parse + gloss))#=三个返回元组的函数




如果你的意思是''word_info'' ,''解析''和''光泽''是三个函数

返回元组,然后通过调用它们获得返回值。

< blockquote class =post_quotes>

def foo():
... return" foo'的返回值"

... def bar (baz):
...返回bar的返回值(包括''%s'')" %baz

... print foo()
foo'的返回值打印栏
<功能栏位于0x401fe80c>打印栏(橙色)



bar'的返回值(包括''orange'')


-

\一个男人必须考虑当他成为一个顺从者时他退位的富裕境界。 - Ralph Waldo Emerson |
_o__)|

Ben Finney


I am writing a program to translate a list of ascii letters into a
different language that requires unicode encoding. This is what I have
done so far:

1. I have ???# -*- coding: UTF-8 -*- as my first line.
2. In Wing IDE I have set Default Encoding to UTF-8
3. I have imported codecs and opened and written my file, which doesn''t
have a BOM, as encoding=UTF-8
4. I have written a dictionary for translation, with entries such as
{''F'':u''\u0254''} and a function to do the translation

Everything works fine, except that my output file, when loaded in
unicode aware emeditor has
(u''F'', u''\u0254'')

But I want to display it as:
(''F'', ''é?'') # where the é? is a back-to-front ''c''

So my questions are:
1. How do I do this?
2. Do I need to change any of my steps above?

解决方案

manstey wrote:

1. I have # -*- coding: UTF-8 -*- as my first line.
2. In Wing IDE I have set Default Encoding to UTF-8
3. I have imported codecs and opened and written my file, which doesn''t
have a BOM, as encoding=UTF-8
4. I have written a dictionary for translation, with entries such as
{''F'':u''\u0254''} and a function to do the translation

Everything works fine, except that my output file, when loaded in
unicode aware emeditor has
(u''F'', u''\u0254'')
I couldn''t quite follow this description: what is "your output file"
(in what step is it created?), and how does

(u''F'', u''\u0254'')

get into this file? What is the precise Python statement that
produces that line of output?
So my questions are:
1. How do I do this?



Most likely, you use (directly or indirectly) the repr() function
to convert a tuple into that string. You shouldn''t do that;
instead, you should format the elements of the tuple yourself, e.g.
through

print >>f, u"(''%s'', ''%s'')" % value

Regards,
Martin


Hi Martin,

HEre is how I write:

input_file = open(input_file_loc, ''r'')
output_file = open(output_file_loc, ''w'')
for line in input_file:
output_file.write(str(word_info + parse + gloss)) # = three
functions that return tuples

(u''F'', u''\u0254'') are two of the many unicode tuple elements returned
by the three functions.

What am I doing wrong?


"manstey" <ma*****@csu.edu.au> writes:

input_file = open(input_file_loc, ''r'')
output_file = open(output_file_loc, ''w'')
for line in input_file:
output_file.write(str(word_info + parse + gloss)) # = three functions that return tuples



If you mean that ''word_info'', ''parse'' and ''gloss'' are three functions
that return tuples, then you get that return value by calling them.

def foo(): ... return "foo''s return value"
... def bar(baz): ... return "bar''s return value (including ''%s'')" % baz
... print foo() foo''s return value print bar <function bar at 0x401fe80c> print bar("orange")


bar''s return value (including ''orange'')

--
\ "A man must consider what a rich realm he abdicates when he |
`\ becomes a conformist." -- Ralph Waldo Emerson |
_o__) |
Ben Finney


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

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