50分:Python读取文本文件处理问题

kalos2012 2009-05-19 12:17:42
加精
有个TXT文件,每行都是中文。现在想用Python打开,f=open('c:\\1.txt').readlines(),可是读取后再print确不能正常显示汉字;如果我要在这个列表f中查找汉字“我”,怎么才能的找到呢 ???
...全文
3641 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
bloodynumen 2011-10-28
  • 打赏
  • 举报
回复
编码问题 亲~ 6楼正解
yoyoyuye 2011-08-17
  • 打赏
  • 举报
回复
makemark
freedomwzq 2011-08-15
  • 打赏
  • 举报
回复
学习下。
grok 2011-08-03
  • 打赏
  • 举报
回复
支持六楼
ljjwf 2010-08-02
  • 打赏
  • 举报
回复
转码是必须的
shiweifu 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hjue 的回复:]
如果文件是gbk的
Python code

# -*- coding: utf-8 -*-
for row in open('c:\\1.txt').readlines():
print row
print row.find(u'我')


如果文件是utf8的
Python code

# -*- coding: utf-8 -*-
for row in open('c:\\……
[/Quote]

学习了
xhnqchchfeng 2010-01-10
  • 打赏
  • 举报
回复
为什么我在3.1下 用6楼的代码不成功呢 (print已加括号)
程高 2010-01-10
  • 打赏
  • 举报
回复
decode
encode
黑咖啡不加糖 2009-12-08
  • 打赏
  • 举报
回复
学习了!!!
armstrong1225 2009-05-27
  • 打赏
  • 举报
回复
up
iwannabe 2009-05-27
  • 打赏
  • 举报
回复
good answer for 6 floor
junzhong 2009-05-26
  • 打赏
  • 举报
回复
:2009-05-19 21:35:472楼 得分:0
要做decode
http://blog.csdn.net/alex197963/archive/2007/04/18/1569207.aspx
lixq2000 2009-05-26
  • 打赏
  • 举报
回复
转码!
不得闲 2009-05-21
  • 打赏
  • 举报
回复
6 楼的正确,使用unicode
梦无痕123 2009-05-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hjue 的回复:]
如果文件是gbk的
Python code#-*- coding: utf-8 -*-forrowinopen('c:\\1.txt').readlines():printrowprintrow.find(u'我')
如果文件是utf8的
Python code#-*- coding: utf-8 -*-forrowinopen('c:\\1.txt').readlines():printrow.decode('utf8').encode('gbk')printrow.find(u'我')


[/Quote]
楼上的正解
多鱼的夏天 2009-05-20
  • 打赏
  • 举报
回复
如果文件是gbk的

# -*- coding: utf-8 -*-
for row in open('c:\\1.txt').readlines():
print row
print row.find(u'我')

如果文件是utf8的

# -*- coding: utf-8 -*-
for row in open('c:\\1.txt').readlines():
print row.decode('utf8').encode('gbk')
print row.find(u'我')


gift_lbs 2009-05-20
  • 打赏
  • 举报
回复
unicode 学习中…
tony_413 2009-05-20
  • 打赏
  • 举报
回复
学习了
Semigod 2009-05-20
  • 打赏
  • 举报
回复
先gbk解码,然后处理,以unicode方式工作才好
armylau 2009-05-19
  • 打赏
  • 举报
回复
要做decode
http://blog.csdn.net/alex197963/archive/2007/04/18/1569207.aspx
加载更多回复(1)

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧