处理任意格式的文本文件

# 内存--硬盘内容  序列话
# 手动挡
# f = open('文件名或类似文件的东西', '文件打卡模式')
# f是文件对象或指针,用来进行读写操作
# f.close()

# 三种模式:
# w. write 写
# r read 读
# a append  追加内容
import os 
%pwd
'C:\\study\\jupyter'
f = open('coop.txt', 'w')  # 用w的方式打开文件,不存在则创建
f.write('coop' * 7)  # 向文件写入字符串
f.close()
with open('coop-1.txt', 'w') as f:
    f.write('coop' * 7)
with open('coop.txt') as f: # 文件名后面的r是默认模式
    data = f.read()# 读出所有内容,保存到一个变量
    print(data)
coopcoopcoopcoopcoopcoopcoop
# 在打开文件时要考虑此文件是否存在,使用try except
with open('coop.txt', 'a') as f:
    f.write('coop1\n')
    f.write('coop2\n')
    f.write('\n111')
    f.write('\n222')
with open('coop.txt') as f:  
    print(f.readline())  # 每次读一行
    print(f.readline())
    print(f.readline())
    print(f.readline())
coopcoopcoopcoopcoopcoopcoopcoop

coop

coop1

coop2
with open('coop.txt') as f:  # 当文件不是很大时用readlines
    print(f.readlines())   # 如何去掉\n
['coopcoopcoopcoopcoopcoopcoopcoop\n', 'coop\n', 'coop1\n', 'coop2\n', '\n', '111\n', '222']
with open('coop.txt') as f:  
    print(f.tell())  # tell()告诉我们光标现在的位置(列的位置)
    print(f.readline())  # 每次读一行
    print(f.tell())
    print(f.readline())
    print(f.tell())
    print(f.seek(0))  # seek(0)让光标返回到初始0的位置
    print(f.readline())
    print(f.readline())
    f.seek(5)

    print(f.readline())
    print(f.tell())
0
coopcoopcoopcoopcoopcoopcoopcoop

34
coop

40
0
coopcoopcoopcoopcoopcoopcoopcoop

coop

oopcoopcoopcoopcoopcoopcoop

34
f = open('coop.txt', 'a')
f.write('append\n')
# print(f.readlines())
7
with open('coop.txt',) as f:
    data = f.read()
    print(data)
coopcoopcoopcoopcoopcoopcoopcoop
coop
coop1
coop2

111
222appendappendappendappendappendappendappend
append
append
append
append
append
##############
# 匹配相应后缀名的文件
import fnmatch
for f in os.listdir('.'):
    if fnmatch.fnmatch(f, '*.txt'):
        print(f)
    elif fnmatch.fnmatch(f, '*.pdf)'):
        print('find pdf', f)
coop-1.txt
coop.txt
# 匹配相应后缀名的文件
import fnmatch
for f in os.listdir('.'):
    if fnmatch.fnmatch(f, '?+.txt'):  # 正则?,一个字符
        print(f)
    elif fnmatch.fnmatch(f, '?.pdf)'):
        print('find pdf', f)
#################
import fnmatch
for f in os.listdir('.'):
    if fnmatch.fnmatch(f, '\w+.txt'):  # 正则?,一个字符
        print(f)
    elif fnmatch.fnmatch(f, '?.pdf)'):
        print('find pdf', f)
# 单纯匹配某种命名规则的文件
import glob
for f in glob.glob('[0-9].txt'):
    print(f)
0.txt
1.txt
import glob
for f in glob.glob('[0-9]+.txt'):  # 不可以加+号,已匹配更多字符
    print(f)
############################
# 序列化 picle ,持久化, 存盘
# 后缀名随意,推荐使用pkl
# 存储python的数据结构
name_list = ['coop', 'fang', 'beijing']
data = {'name':name_list, 'age':(2,3,4)}
import pickle
with open('data.pkl', 'wb') as f: # 使用wb,通用二进制存储
    pickle.dump(data, f)
with open('data.pkl', 'rb') as f:
    data = pickle.load(f)
    print(data)
{'name': ['coop', 'fang', 'beijing'], 'age': (2, 3, 4)}
############################
# 虚拟文件,临时文件,不需要真的存到磁盘
import io
output = io.StringIO()
output.write('the first code\n')
print('ddd', file=output)

# 去除内容
contents = output.getvalue()
print(contents)

#关闭文件,清理缓存
output.close()   # 打印顺序为什么是那个样子
the first code
ddd
# 用类似字典的方式存储任意的python对象  pickle存储的是数据结构
import shelve
with shelve.open('coop.she') as so:
    so['coop'] = 'fang'  # 生成三个文件
with shelve.open('coop.she') as so:
    print(so['coop'])
fang

猜你喜欢

转载自blog.51cto.com/13118411/2138907