Python 文件编码(文件乱码)
2015-11-09 17:15
711 查看
IndentationError: unindent does not match any outer indentation level
文件未对齐,在记事本打开。
乱码原因:
源码文件的编码格式为utf-8,但是window的本地默认编码是gbk,所以在控制台直接打印utf-8的字符串当然是乱码了!
解决方法:
1、print mystr.decode('utf-8').encode('gbk')
2、比较通用的方法:
复制代码代码如下:
import sys
type = sys.getfilesystemencoding()
print mystr.decode('utf-8').encode(type)
发到哈工大
打开文件:
建立磁盘上的文件与程序中的对象相关联
通过相关的文件对象获得
读取
写入
定位
其他:追加,计算等
关闭文件
切断文件与程序的联系
写入磁盘,并释放文件缓冲区
打开文件:
open()
<variable> = open (<name>,<mode>)
<name>磁盘文件名
<mode>打开模式
egg:
打开一个名为“numbers.dat”的文本文件
>>>infile = open ("numbers.dat","r")
打开一个名为“music.mp3”的音频文件
>>>infile = open("music.mp3","rb")
def main()"
fname = input ("Enter filename:")
infile = open(fname,"r")
data = infile.read()
print(data)
main()
infile = open(someFile,"r")
for i in range(5):
line = infile.readline()
print(line[:-1])
>>>outfile = open("outfile.txt","w")
>>>outfile .writelines(["Hello"," ","world"])
>>>outfile.close()
>>>infile = open("outfile.txt","r")
>>>infile.read()
'Hello world'
python 读文件中的数字利用turtle画图
http://www.icourse163.org/learn/bit-268001?tid=317001#/learn/content?type=detail&id=872018&sm=1
文件未对齐,在记事本打开。
乱码原因:
源码文件的编码格式为utf-8,但是window的本地默认编码是gbk,所以在控制台直接打印utf-8的字符串当然是乱码了!
解决方法:
1、print mystr.decode('utf-8').encode('gbk')
2、比较通用的方法:
复制代码代码如下:
import sys
type = sys.getfilesystemencoding()
print mystr.decode('utf-8').encode(type)
发到哈工大
打开文件:
建立磁盘上的文件与程序中的对象相关联
通过相关的文件对象获得
读取
写入
定位
其他:追加,计算等
关闭文件
切断文件与程序的联系
写入磁盘,并释放文件缓冲区
打开文件:
open()
<variable> = open (<name>,<mode>)
<name>磁盘文件名
<mode>打开模式
egg:
打开一个名为“numbers.dat”的文本文件
>>>infile = open ("numbers.dat","r")
打开一个名为“music.mp3”的音频文件
>>>infile = open("music.mp3","rb")
def main()"
fname = input ("Enter filename:")
infile = open(fname,"r")
data = infile.read()
print(data)
main()
infile = open(someFile,"r")
for i in range(5):
line = infile.readline()
print(line[:-1])
>>>outfile = open("outfile.txt","w")
>>>outfile .writelines(["Hello"," ","world"])
>>>outfile.close()
>>>infile = open("outfile.txt","r")
>>>infile.read()
'Hello world'
python 读文件中的数字利用turtle画图
http://www.icourse163.org/learn/bit-268001?tid=317001#/learn/content?type=detail&id=872018&sm=1
相关文章推荐
- python dic与str的相互转化
- Python核心数据类型——数字
- Python 常用函数time.strftime()简介
- 使用Python发送各种形式的邮件的方法汇总
- python程序里加入调试断点
- Python3.4(Pyhon)代码如何打包成.exe可执行文件——详细教程
- Python 碎片
- python更新后yum报错SyntaxError: invalid syntax
- 更新了jdk和python的版本,eclipse启动不了怎么破
- python中字典的排序和复制
- Python函数式编程指南(一):概述
- python 爬虫 <class 'socket.error'>: [Errno 104] Connection reset by peer)
- Python学习之路六---迭代器、生成器
- Python SocketServer.py
- 【详解】Python统一解密再改进版逻辑分析
- 《用Python玩转数据》第1周学习笔记(Part2)
- 尝试使用Python多线程抓取代理服务器IP地址的示例
- 关于selenium 用Webdrive打开Firefox不含有插件的问题(python)
- 使用Python实现BT种子和磁力链接的相互转换
- Python中MySQLdb和torndb模块对MySQL的断连问题处理