|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在做如何读取txt文件内容的练习时,遇到了一些问题,后经搜索、分析、学习,觉得有些收获,记录一下,也希望能够帮助到遇到同样问题的同学。
两个问题:
1、txt文件中有中文时,会报这个错误
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 87: illegal multibyte sequence
解决方法很简单,在打开文件时加上编码属性即可
with open(dir, 'r',encoding = 'utf-8') as f:
2、文件(f)用 for...in... 结构迭代时,f.readline()的读取问题,大家自己可以分析一下第三个函数 readFileText3(),其实很简单。
程序示例:
- def readFileText1(dir):
- texts = []
- with open(dir, 'r',encoding = 'utf-8') as f:
- for line in f:
- texts.append(line.strip('\n'))
- return texts
-
- #==================================================================
-
- def readFileText2(dir):
- texts = []
- with open(dir, 'r',encoding = 'utf-8') as f:
- for i in range(0, 10):
- texts.append(f.readline().strip('\n'))
- return texts
-
- #===================================================================
-
- def readFileText3(dir):
- texts = []
- with open(dir, 'r',encoding = 'utf-8') as f:
- for line in f:
- texts.append(f.readline().strip('\n'))
- return texts
-
- textdir = 'D:\\PythonTest\\IDLE\\testone.txt'
- filetexts1 = readFileText1(textdir)
- filetexts2 = readFileText2(textdir)
- filetexts3 = readFileText3(textdir)
- print(filetexts1)
- print('=============================================')
- print(filetexts2)
- print('*********************************************')
- print(filetexts3)
复制代码
运行结果
- #运行结果
-
- ['dfdfdsfasf', 'sdfweewrewrkl', 'dsf[ospo', 'sdflkjj', 'kl;ipm,.m,', 'xiuuyifdyu', 'syuywwpoqasdf', '中文实例']
- =============================================
- ['dfdfdsfasf', 'sdfweewrewrkl', 'dsf[ospo', 'sdflkjj', 'kl;ipm,.m,', 'xiuuyifdyu', 'syuywwpoqasdf', '中文实例', '', '']
- *********************************************
- ['sdfweewrewrkl', 'sdflkjj', 'xiuuyifdyu', '中文实例']
复制代码
|
|