欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

處理任意格式的文本文件

# 內(nèi)存--硬盤內(nèi)容  序列話
# 手動擋
# f = open('文件名或類似文件的東西', '文件打卡模式')
# f是文件對象或指針,用來進(jìn)行讀寫操作
# f.close()

# 三種模式:
# w. write 寫
# r read 讀
# a append  追加內(nèi)容
import os 
%pwd
'C:\\study\\jupyter'
f = open('coop.txt', 'w')  # 用w的方式打開文件,不存在則創(chuàng)建
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是默認(rèn)模式
    data = f.read()# 讀出所有內(nèi)容,保存到一個變量
    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:  # 當(dāng)文件不是很大時用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()告訴我們光標(biāo)現(xiàn)在的位置(列的位置)
    print(f.readline())  # 每次讀一行
    print(f.tell())
    print(f.readline())
    print(f.tell())
    print(f.seek(0))  # seek(0)讓光標(biāo)返回到初始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
##############
# 匹配相應(yīng)后綴名的文件
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
# 匹配相應(yīng)后綴名的文件
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)
# 單純匹配某種命名規(guī)則的文件
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的數(shù)據(jù)結(jié)構(gòu)
name_list = ['coop', 'fang', 'beijing']
data = {'name':name_list, 'age':(2,3,4)}
import pickle
with open('data.pkl', 'wb') as f: # 使用wb,通用二進(jìn)制存儲
    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)

# 去除內(nèi)容
contents = output.getvalue()
print(contents)

#關(guān)閉文件,清理緩存
output.close()   # 打印順序?yàn)槭裁词悄莻€樣子
the first code
ddd
# 用類似字典的方式存儲任意的python對象  pickle存儲的是數(shù)據(jù)結(jié)構(gòu)
import shelve
with shelve.open('coop.she') as so:
    so['coop'] = 'fang'  # 生成三個文件
with shelve.open('coop.she') as so:
    print(so['coop'])
fang

網(wǎng)站標(biāo)題:處理任意格式的文本文件
轉(zhuǎn)載來源:http://chinadenli.net/article46/jhgshg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站搜索引擎優(yōu)化網(wǎng)站內(nèi)鏈微信公眾號虛擬主機(jī)商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司