上文是提到关于PyQt的QTextStream类读写文本简要说明。如果利用python标准库编程时,python还提供了用于文本读写的内置类和标准库。使用这些文件类,可以直接读写文件,不过,如果打指定编码方式,就必须使用codes模块。
这个模块对文本的操作与QTextStream非常类似。与QTextStream同样得特别注意文本格式编码的问题,读取编码和写出编码方式如果存在不同,则会造成相关数据的误读。
以下例子为创建一个类型结构Movie,然后将此类型通过codes模块保存到指定的文本文件,然后从文本文件将保存的内容读出,文本文件的编码为UTF-8,文本的内容格式如下:
{{MOVIE}} God save world
1989 45 2017-01-14
{NOTES}
HELLO WORLD
{{ENDMOVIE}}
import codecs
import datetime
import os
CODEC = "UTF-8"
class Movie(object):
UNKNOWNYEAR = 1890
UNKNOWNMINUTES = 0
def __init__(self, title=None, year=UNKNOWNYEAR,
minutes=UNKNOWNMINUTES, acquired=None, notes=None):
self.title = title
self.year = year
self.minutes = minutes
self.acquired = (acquired if acquired is not None
else datetime.date.today())
self.notes = notes
class MovieContainer(object):
def __init__(self,fname,movies):
self.__fname = fname
self.__movies = movies
def saveText(self):
error = None
fh = None
try:
fh = codecs.open(str(self.__fname), "w", CODEC)
movie=self.__movies
fh.write("{{MOVIE}} ")
fh.write("{0}\n".format(str(movie.title)))
fh.write("{0} {1} {2}\n".format(movie.year, movie.minutes,
movie.acquired))
fh.write("{NOTES}")
if movie.notes:
fh.write("\n{0}".format(str(movie.notes)))
fh.write("\n{{ENDMOVIE}}\n")
except EnvironmentError as e:
error = "Failed to save: {0}".format(e)
finally:
if fh is not None:
fh.close()
if error is not None:
print(error)
print("Saved 1 movie records to {0}".format(
os.path.basename(self.__fname)))
def loadText(self):
error = None
fh = None
try:
fh = codecs.open(str(self.__fname), "rU", CODEC)
lino = 0
while True:
title = year = minutes = acquired = notes = None
line = fh.readline()
if not line:
break
lino += 1
if not line.startswith("{{MOVIE}}"):
raise ValueError("no movie record found")
else:
title = line[len("{{MOVIE}}"):].strip()
print(title)
line = fh.readline()
if not line:
raise ValueError("premature end of file")
lino += 1
parts = line.split(" ")
if len(parts) != 3:
raise ValueError("invalid numeric data")
year = int(parts[0])
minutes = int(parts[1])
print(year)
print(minutes)
ymd = parts[2].split("-")
if len(ymd) != 3:
raise ValueError("invalid acquired date")
acquired = datetime.date(int(ymd[0]), int(ymd[1]),
int(ymd[2]))
print(acquired)
line = fh.readline()
if not line:
raise ValueError("premature end of file")
lino += 1
if line != "{NOTES}\n":
raise ValueError("notes expected")
notes = ""
while True:
line = fh.readline()
if not line:
raise ValueError("missing endmovie marker")
lino += 1
if line == "{{ENDMOVIE}}\n":
if (title is None or year is None or
minutes is None or acquired is None or
notes is None):
raise ValueError("incomplete record")
break
else:
notes += line
print(notes)
except (IOError, OSError, ValueError) as e:
error = "Failed to load: {0} on line {1}".format(e, lino)
finally:
if fh is not None:
fh.close()
if error is not None:
print(error)
print("Loaded 1 movie records from {0}".format(
os.path.basename(self.__fname)) )
if __name__ == "__main__":
textdata=[["God save world",1989,45,None,"HELLO WORLD"]]
fname="/home/yrd/work/movie.codecs"
for data in textdata:
movie=Movie(data[0],data[1],data[2],data[3],data[4])
moviecontainer=MovieContainer(fname, movie)
moviecontainer.saveText()
moviecontainer.loadText()
运行结果:
Saved 1 movie records to movie.codecs
God save world
1989
45
2017-01-14
HELLO WORLD
Loaded 1 movie records from movie.codecs