且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Unicode问题-HELP

更新时间:2023-09-11 21:09:40

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'' ,''解析''和''光泽''是三个函数

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

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