python語言 寫入檔案

來源:互聯網
上載者:User

標籤:trunc   range   lines   大資料檔案   修改   with open   zhang   date   變化   

# author:zhangxiaomeng
# date:2017/12/4


############################基本流程
#能調用方法的一定是對象
# li=[1,2,3]
# li.append(‘2‘)
# ‘asc‘.capitalize()

# import time
#三種基本的操作模式 r(只可讀) w(只可寫) a(追加)
#流程:1 建立檔案對象 2 調用檔案方法進行操作 3 關閉檔案

# f=open(‘小重山2‘,‘r‘,encoding=‘utf8‘)
# f.write()#報錯

# data=f.read(5)
# print(data)
# f.write(‘\nhello world \n‘)
# f.write(‘alex‘)
#
#注意:if not close,資料會緩衝,而不是磁碟!
# time.sleep(30)
# f.close()




################################具體操作方法

# f=open(‘小重山‘,‘a‘,encoding=‘utf8‘)

# print(f.read(5))
# print(f.read(5))# 注意 :漢字在這裡佔一個單位(in Py3)

# a=f.readline()
# print(a)

# print(f.readline())
# print(f.readline())#無論是read()還是readline(),游標會發生位置變化

# print(f.readlines())#[‘昨夜寒蛩不住鳴。\n‘, ‘驚回千裡夢,已三更。\n‘, ‘起來獨自繞階行。\n‘, ‘人悄悄,簾外月朧明。\n‘, ‘白首為功名,舊山松竹老,阻歸程。\n‘, ‘欲將心事付瑤琴。\n‘, ‘知音少,弦斷有誰聽。‘]



#執行個體列印檔案

# data=f.readlines()#注意及時關閉檔案
# f.close()

# number = 0
# for i in data:
# number += 1

# if number == 6:
# i = ‘‘.join([i.strip(), ‘iiiii‘]) # 取代萬惡的+
# print(i.strip())
# f.close()

##########對於大資料檔案,要用以下方式(the best way):
# number=0
# for i in f:#這是for內部將f對象做成一個迭代器,用一行去一行。
# number+=1
# if number == 6:
# i = ‘‘.join([i.strip(), ‘iiiii‘]) # 取代萬惡的+
# # print(i.strip())
# print(i.strip())



# print(f.tell())# 取出游標位置
# print(f.read(2))
# print(f.tell())
#
# f.seek(0)# 移動游標到指定的位置
# print(f.read(4))

#flush():同步吧將資料從緩衝轉移到磁碟上去
##進度條執行個體
# import sys,time
# for i in range(30):
# sys.stdout.write("*")
# sys.stdout.flush()
# time.sleep(0.1)


#print的flush
# import sys,time
# for i in range(30):
#
# print(‘*‘,end=‘‘,flush=True)
#
# time.sleep(0.1)
# f=open(‘小重山‘,‘r‘,encoding=‘utf8‘)


#truncate():截斷資料(不能在r模式下)
#在w模式下:先清空,再寫,再截斷
#在a模式下:直接將指定位置後的內容截斷
# f.truncate(5)
# f.write(‘hello world‘)
# f.truncate(5)
# f.close()


##### r+, w+, a+

# r+:游標預設在0位置,最後位置開始寫
# w+:先清空,再寫讀
# a+:游標預設在最後位置


# f=open(‘小重山‘,‘r+‘,encoding=‘utf8‘)
# print(f.tell())
# print(f.readline())
# f.write(‘嶽飛‘)
# print(f.tell())
# f.seek(0)
# print(f.readline())
# f.close()

# 終極問題 如何在磁碟修改檔案
# 常規思路,由於磁碟儲存機制不能完成
# number = 0
# for line in f:
# number += 1
# if number == 3:
# f.write(‘alex‘)


# 只能採取重新建立一個檔案的思路
f_read = open(‘小重山‘, ‘r‘, encoding=‘utf8‘)
f_write = open(‘小重山2‘, ‘w‘, encoding=‘utf8‘)

number = 0
for line in f_read:
number += 1
if number == 5:
line = ‘‘.join([line.strip(), ‘alex\n‘])
# line=‘hello 嶽飛\n‘
f_write.write(line)


f_read.close()
f_write.close()


# #####作業涉及方法
# a=str({‘beijing‘:{‘1‘:111}})
# print(type(a))
# print(a)# ‘{‘beijing‘:{‘1‘:111}}‘
# a=eval(a)
# print(type(a))
# print(a[‘beijing‘])

# f=open(‘log‘, ‘r‘)
# f.readline()
# f.read()
# f.close()

# with open(‘log‘, ‘r‘) as f:
# f.readline()
# f.read()
# print(‘hello‘)

#with 同時管理多個檔案對象
# with open(‘log1‘,‘r‘) as f_read, open(‘log2‘,‘w‘) as f_write:
# for line in f_read:
# f_write.write(line)









































python語言 寫入檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.