python列表输出字符串,如何在python中输出utf-8字符串列表?
Well, character encoding and decoding sometimes frustrates me a lot.
So we know u'\u4f60\u597d' is the utf-8 encoding of 你好,
>>> print hellolist
[u'\u4f60\u597d']
>>> print hellolist[0]
你好
Now what I really want to get from the output or write to a file is [u'你好'], but it's [u'\u4f60\u597d'] all the time, so how do you do it?
解决方案
When you print (or write to a file) a list it internally calls the str() method of the list , but list internally calls repr() on its elements. repr() returns the ugly unicode representation that you are seeing .
Example of repr -
>>> h = u'\u4f60\u597d'
>>> print h
\u4f60\u597d
>>> print repr(h)
u'\u4f60\u597d'
You would need to manually take the elements of the list and print them for them to print correctly.
Example -
>>> h1 = [h,u'\u4f77\u587f']
>>> print u'[' + u','.join([u"'" + unicode(i) + u"'" for i in h1]) + u']'
For lists containing sublists that may have unicode characters, you would need a recursive function , example -
>>> h1 = [h,(u'\u4f77\u587f',)]
>>> def listprinter(l):
... if isinstance(l, list):
... return u'[' + u','.join([listprinter(i) for i in l]) + u']'
... elif isinstance(l, tuple):
... return u'(' + u','.join([listprinter(i) for i in l]) + u')'
... elif isinstance(l, (str, unicode)):
... return u"'" + unicode(l) + u"'"
...
>>>
>>>
>>> print listprinter(h1)
To save them to file, use the same list comprehension or recursive function. Example -
with open('','w') as f:
f.write(listprinter(l))
python列表输出字符串,如何在python中输出utf-8字符串列表?相关推荐
- python字符串筛选输出_如何在Python中过滤字符串列表
Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...
- 如何在Go中编写多行字符串?
本文翻译自:How do you write multiline strings in Go? Does Go have anything similar to Python's multiline ...
- html img调用js,html调用js变量 如何在html中输出js文件中的变量
html页面代码中怎么调用js变量?html页面代码中怎么调用js变量,例如 在html代码中插入js代码: a=取浏览你把index1.js 中的onReady 去掉,把index1.js改成 fu ...
- regexp 好汉字符串_如何在JavaScript中使用RegExp确认字符串的结尾
regexp 好汉字符串 by Catherine Vassant (aka Codingk8) 由凯瑟琳·瓦森(Catherine Vassant)(又名Codingk8) 如何在JavaScrip ...
- 如何在printf中输出,特殊字符(如:%、\、““)或表示八进制012、十六进制0xc
如何在printf中输出: 特殊字符(如:%.\."")或表示八进制012.十六进制0xc #include <stdio.h>int main(void) {floa ...
- 不使用strcpy函数,编程实现把b字符串复制到a中的程序(字符串长度限制在100个字符以内)。以下是部分代码,请完成并提交
不使用strcpy函数,编程实现把b字符串复制到a中的程序(字符串长度限制在100个字符以内).以下是部分代码,请完成并提交. #include <stdio.h> int main(){ ...
- unbantu上python安装步骤_如何在Ubuntu中安装Python 3.6?
Python是增长最快的主要通用编程语言.原因有很多,比如它的可读性和灵活性,易于学习和使用,可靠和高效. 有两个主要的Python版本被使用- 2和3 (Python的现在和未来);前者将看不到新的 ...
- python 线性回归模型_如何在Python中建立和训练线性和逻辑回归ML模型
python 线性回归模型 Linear regression and logistic regression are two of the most popular machine learning ...
- Python 换行符以及如何在 Python 输出时不换行
Python 中的换行符用于标记行的结尾和新行的开始.如果你想将输出打印到控制台并使用文件,那么你非常需要知道如何使用它. 在本文中,你将学习: 如何在 Python 中识别换行符 如何在字符串和打印 ...
- python大括号用法_Python如何在 .format 中使用大括号
python中字符串的format功能非常强大,可以说完全能够替代其他的字符串使用方法 format功能举例: format的大括号用法可以实现字符串的拼接 大括号内可以打乱顺序,根据{}里面的顺序写 ...
最新文章
- vijos 1476 旅游规划题解
- 《Linux内核设计与实现》读书笔记(六)- 内核数据结构
- 【算法分析与设计】实验 回溯算法解决0-1背包问题
- mysql 4.0手工注入_手工注入——MySQL手工注入实战和分析
- mysql 函数怎样创建_mysql里怎样创建函数
- 长得像鳗鱼的Envirobot,利用传感器检测并追踪水中有害元素
- python书籍_最好的Python书籍,让您像专业程序员一样编程
- vue 父组件使用keep-alive和infinite-scroll导致在子组件触发父组件的infinite-scroll方法...
- C#之获取网卡IP地址
- 大华海康宇视等监控安防摄像头如何通过GB/T28181平台接入到GB28181国标流媒体平台
- 谷歌推出 Translatotron 2,一种没有深度伪造潜力的语音到语音直接翻译神经模型
- 小米手机录制数据集软件操作
- 2020美团笔试题目:送餐小区数量
- 极客爱情前传:程序员应该送什么礼物给女朋友
- 心随风起,葬身荒野亦无悔;志在止戈,醉卧沙场最逍遥。
- R语言 eval parse 字符串内有引号 格式化输出
- 大数据进阶之路——Spark SQL 之 DataFrameDataset
- win10打不开cmd,windows找不到文件cmd怎么办?
- 1.虚拟机克隆后的处理步骤
- 教你如何处理加速电脑速度(转载)
热门文章
- 《人工智能如何走向新阶段》大家谈(跟帖,续)
- 线上四台机器同一时间全部 OOM,到底发生了什么?
- 互联网始于 50 年前的 3420 室
- 前端开发这么多年,你真的了解浏览器页面渲染机制吗? | 技术头条
- 戴尔科技集团公布 2019 财年第四季度及全年财报,巨大进步和强劲发展的一年...
- 亚马逊不仅将弃用 Oracle,还要抢 Java 饭碗!
- Facebook 正式公开 React Native 重构细节!
- 真相:Java 开发者钟爱 Kotlin 的五个原因
- 自己java_一些自己用的java类
- 怎么让照片变年轻_做了隆鼻,至少年轻5岁