English 中文(简体)
将 Unicode 字符串转换为原始格式 [重复]
原标题:Convert a unicode string to its original format [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Converting a latin string to unicode in python

保存在文件后,我有一份列表,格式如下:

list_example = [
         u"u00cdndia, Tailu00e2ndia & Cingapura",
         u"Lines through the days 1 (Arabic) u0633u0637u0648u0631 u0639u0628u0631 u0627u0644u0623u064au0627u0645 1",
]

但列表中字符串的实际格式是

actual_format = [
         "Índia, Tailândia & Cingapura ",
         "Lines through the days 1 (Arabic) سطور عبر الأيام 1 | شمس الدين خ "
]

我如何才能将 list_example 中的字符串转换为 actual_format 列表中的字符串?

最佳回答

你的问题对我来说有点不清楚,无论如何,以下准则应该有助于解决你的问题。

如果您在 Python 源代码中定义这些字符串, 那么您应该

  • know in which character encoding your editor saves the source code file (e.g. utf-8)
  • declare that encoding in the first line of your source file, via e.g. # -*- coding: utf-8 -*-
  • define those strings as unicode objects:

strings = [u "andia, Tailândia & amp; Cingapura", u " lines through the days 1 (阿拉伯语) {\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\> <\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

(注:在 Python 3 中, 字串默认为 Unicode 对象, 也就是说, 您不需要 < code> > u 。 在 Python 2 中, unicode 字符属于 < code> unicode 类型, 在 Python 中, 3 Unicode 字符串属于 < code> string 类型。 )

当您想要将这些字符串保存到文件时, 您应该明确定义字符编码 :

with open( filename ,  w ) as f:
    s =  
 .join(strings)
    f.write(s.encode( utf-8 ))

当您想要从该文件中再次读取这些字符串时, 您必须再次明确定义字符编码, 以便正确解码文件内容 :

with open( filename ) as f:
    strings = [l.decode( utf-8 ) for line in f]
问题回答
actual_format = [x.decode( unicode-escape ) for x in list_example]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签