1、 读写文件
if __name__== "__main__":
filename = input("Please input the name of file:")
f = open(filename,"w")
while 1:
context = input("Please input context('EOF' will close file): ")
if context == "EOF":
f.close()
break
else:
f.write(context)
f.write("\n")
fRead = open(filename)
readContext = fRead.read()
print("------------start-------------")
print(readContext)
print("-------------end--------------")
fRead.close()
运行结果:
Please input the name of file:z.log
Please input context('EOF' will close file): hello
Please input context('EOF' will close file): the weather is cool
Please input context('EOF' will close file): you have wear more clothes
Please input context('EOF' will close file): EOF
------------start-------------
hello
the weather is cool
you have wear more clothes
-------------end--------------
2、 读取文件方法
import codecs
ENCODING = "utf-8"
f = open("z.log",encoding=ENCODING)
print(f.name)
print(f.readline())
print(f.readlines())
with codecs.open("z.log","r",encoding=ENCODING) as f:
print(f.read())
3、 编码问题
编码:
支持中文的编码:utf-8,gbk,gb2312
decode 解码
encode 编码
在Python2中不定义代码的编码排头,在内容中出现中文时会报错。
Python默认将代码文件内容当做ASCII编码处理,但是ASCII编码不存在中文,因为则会抛出异常。
解决问题之道就是要让Python之道文件中使用的是什么编码形式,对于中文,可以用的常见编码有utf-8,gbk和gb2312等,只需在代码文件的最前端添加如下内容即可:
# -*- coding:utf-8 -*-
Python转码的过程:
原有编码 ——> Unicode编码 ——> 目的编码
python会自动将带中文的字符串解码成Unicode,然后再编码成gbk,因为解码是字典进行的,如果没有指明解码方式,就会使用sys,defaultencoding指明的方式来解码。
方法一:
s.decode("utf-8").encoding("gbk")
4、对文件进行排序
import codecs
file = "passwd"
sortfile = "sortpasswd.txt"
filecontext = []
sortuid = []
with codecs.open(sortfile,"wb") as fsort:
with codecs.open(file,encoding="utf-8") as f:
filecontext += f.readlines()
for line in filecontext:
sortuid.append(int(line.split(":")[2]))
sortuid.sort()
for uid in sortuid:
for line in filecontext:
if str(uid) == line.split(":")[2]:
print(line)
fsort.write(line.encode("utf-8"))
python3的新特性对文本和二进制数据作了更为清晰的区分,
文本总是Unicode,由str类型表示,
二进制则是由bytes类型表示
字符串可以encode编码成字节包,而字节包可以decode解码成字符串
本文转自 归来仍少年 51CTO博客,原文链接:http://blog.51cto.com/shaoniana/2065990