新手剛剛開始學習python,如有寫錯或者寫的不好的地方,請大家多多指導!
python元組相加
a = (1,2)
b = (3,4)
a + b
元組運用乘法
(1,2) * 4 #在這裏邊,元組不會被當成數字來計算,而是輸出4次
給字母類型的元組拍
t = ('bb,','dd','aa','cc')
tm = list(t)
tm.sort() #然後輸出tm
t = tuple(tm)
用for的方式運算
t = (1,2,3,4,5)
l = [x + 20 for x in t]
替換元組
t = (1,[2,3],4)
t[1][0] = 'spa' #t元組中第二個數值之後緊挨着的數值
python文件操作
常見的文件運算
output = open(r'd:\a.py', 'w') 創建輸出文件(w是指寫入)
input = open('date', 'r') 創建輸入文件(r是指讀寫)
input = open('date') 與上一行想同(r是默認值)
input.read() 把整個文件讀取進單一字符串
input.read(N) 讀取之後的N個字節,到一個字符串
input.readline() 逐行讀取,第一次讀取第一行,第二次讀取下一行
alist = input.readlines() 讀取整個文件到字符串列表
output.write(as) 寫入字節字符串到文件
output.writelines(alist) 把列表內所有字符串寫入文件
output.close() 手動關閉(當文件收集完成是會替你關閉文件)
output.flush() 把輸出緩衝區刷到硬盤中,但不關閉文件
anyFile.seek(N) 修改文件位置到偏移量N處以便進行下一個操作
for line in open('data'): use line 文件迭代器一行一行的讀取
open('f.txt', encoding='latin-1') python3.0unicode文本文件(str字符串)
open('f.bin', 'rb') python3.0二進制byte文件(bytes字符串)
實例應用
myfile = open('myfile.txt', 'w') #創建一個myfile.txt文件,並打開進行寫入
myfile.write('hello,world\n')
myfile.write('good bye'\n) #\n表示轉行
myfile.close() #關閉文件 然後打開本地目錄,看看文件內容是否一樣
讀取文件
myfile = open('myfile.txt') #打開文件,默認是隻讀
myfile.readline() #讀取第一行
myfile.readline() #讀取下一行
把整個文件讀取進單一字符串
open('myfile.txt').read() #把所以文件一次性讀取完,\n之後的表示下一行
使用打印的方式來讀取
print(open('myfile.txt').read()) #這樣處理的結果比較清晰,隔行分開
用for的方式來逐行讀取文件
for line in open('myfile.txt'):
print(line,end='')
以二進制的方法打開文件
data = open('myfile.txt', 'rb').read() #這樣的話效果不太明顯,可以創建文本寫入數字開看看
data[4:8]
data[0]
bin(data[0]) #二進制的方式顯示一個文件
文件存儲
x, y, z = 43, 44, 45
s = 'spam'
d = {'a': 1,'b': 2}
l = [1,2,3]
f = open('data.txt', 'w')
f.write(s + '\n') #直接將s插入然後轉行
f.write('%s,%s,%s\n' % (x,y,z))
f.write(str(l) + '$' str(d) + '\n') #str輸出l + str輸出的d
然後讀取看下結果
a = open('data.txt').read()
print(a)
去掉多餘的行
f = open('data.txt')
line = f.readline()
line.rstrip() #移除掉用了轉行的\n
創建2進制文件
d = {'a': 1,'b': 2}
f = open('datafile.pk', 'wb')
import pickle
pickle.dump(d, f)
f.close()
想讀取的時候,因爲轉換成二進制了,還要用pickle讀取回來
f = open('datafile.pk', 'rb')
a = pickle.load(f) #在這不知道是什麼原因,有時候這樣定義的話會報錯
pickle.load(f) #如果報錯的話,就這樣來讀取
直接打開二進制文件
open('datafile.pk', 'rb').read() #顯示的就是一堆二進制數字,而不是插入的數值
使用struct模塊來進行二進制文件的打包和解析
首先來進行創建
f = open('data.bin', 'wb')
import struct
data = struct.pack('>i4sh',7,'spam', 8)
f.write(data)
f.close()
然後讀取
f = open('data.bin', 'rb')
data = f.read()
values = struct.unpack('i4sh', data) #然後輸出values
新手剛剛開始學習python,如有寫錯或者寫的不好的地方,請大家多多指導!
python元組,文件的操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
Python3高级核心技术97讲
wy53780
2020-04-23 15:37:39
python網絡爬蟲
aabcdqt
2019-05-06 20:35:05
Python與家國天下
豌豆花下貓
2019-02-24 22:22:40
seaborn 安裝成功 + ImportError: DLL load failed: 找不到指定的模塊 問題解決
耗子
2019-02-24 19:55:04
使用 Sphinx 撰寫技術文檔並生成 PDF 總結
qiwihui
2019-02-24 16:25:14
Python實現二叉樹相關算法
backbp
2019-02-24 15:35:06
Ubuntu16.04安裝Python3.7及其pip3並切換爲默認版本
ipromise
2019-02-24 14:15:00
Python2+Selenium入門05-關於WebElement類
白雪
2019-02-24 13:45:04
Python——數據類型
陳晨工程師
2019-02-24 13:43:55
Python 操作PDF庫介紹之PDFMiner
iCloudEnd
2019-02-24 13:39:59
Python reportlab 之 draw函數介紹
iCloudEnd
2019-02-24 13:39:59
Python reportlab之pdfgen 介紹(含代碼)
iCloudEnd
2019-02-24 13:39:59
Python手繪圖瞭解一下!
python攻城獅
2019-02-24 13:34:06
Python繪製圖表【matplotlib】
流域哈哈
2019-02-24 13:20:58
Python2+Selenium入門04-關於WebDriver類
白雪
2019-02-24 12:45:06