如何将非ascii字符串的unicode表示转换为python字符串?

时间:2016-11-17 15:29:35

标签: python string unicode

如何将非ascii范围字符串的unicode表示转换为python字符串?

> str = '<some-arabic-content>'
> decoded_str = str.decode('utf-8')
> decoded_str
u'\u0623\u0646\u0633\u0646\u0629'
> str(decoded_str)
<WILL-THROW-AN-ERROR-OFC> since ascii cannot encode the unicode string

如何将此表示形式(&#39; \ u0623 \ u0646 \ u0633 \ u0646 \ u0629&#39;)作为python字符串?如下所示。

> str_rep = '\u0623\u0646\u0633\u0646\u0629'

我想从str_rep登陆decoded_str。我怎么能这样做呢?

2 个答案:

答案 0 :(得分:1)

<properties> <activemq.version>5.14.1</activemq.version> <camel.version>2.18.0</camel.version> <infinispan.version>8.3.0.Final-redhat-1</infinispan.version> <camel-jbossdatagrid.version>6.6.1.Final-redhat-1</camel-jbossdatagrid.version> </properties> <dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jackson</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jsonpath</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-embedded</artifactId> <version>${infinispan.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jbossdatagrid</artifactId> <version>${camel-jbossdatagrid.version}</version> </dependency> <!--ActiveMQ --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-camel</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm-all</artifactId> <version>5.1</version> </dependency> </dependencies> <repositories> <repository> <id>fuse-release</id> <name>jboss Release Repository</name> <releases> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <url>http://repo.fusesource.com/nexus/content/groups/public/</url> </repository> </repositories> </project> 产生所需的输出。

答案 1 :(得分:0)

您是否尝试打印变量encoded_str ??

foo = u'\u0623\u0646\u0633\u0646\u0629'

print(foo)

أنسنة

print(repr(foo))
u'\u0623\u0646\u0633\u0646\u0629'