python 循环写文件_循环-读写文件-字符编码
目錄:
1.1 while與for循環
1、賦值魔法
#1. 序列解包: 將多個值的序列解開,然后放到序列的變量中。
x,y,z = 1,2,3
print(x,y,z) #the result : 1 2 3
x,y = y,x
print(x,y,z) #the result : 2 1 3
#2. 鏈式賦值: 將同一個值賦給多個變量的捷徑
x = y = z = 110
print(x,y,z) #the result :110 110 110
#1. 序列解包: 將多個值的序列解開,然后放到序列的變量中。
x,y,z = 1,2,3
print(x,y,z) #the result : 1 2 3
x,y = y,x
print(x,y,z) #the result : 2 1 3
#2. 鏈式賦值: 將同一個值賦給多個變量的捷徑
x = y = z = 110
print(x,y,z) #the result :110 110 110
2、更復雜的條件
#1. 比較運算符
x != y x不等于y
x is y x和y是同一個對象
x is not y x和y是不同的對象
x in y x是y容器(例如序列)的成員
x not in y x不是y容器(例如序列)的成員
#2. is:同一性運算
#作用:is運算符看起來和==一樣,事實上卻不同
x = y = [1,2,3]
z = [1,2,3]
print(x==y==z) #the result : True
print(x is y) #the result : True
print(x is z) #the result : False
#3. in: 成員資格運算符
name = input('what is your name: ')
if 's' in name:
print('your name contains the letters "s" ')
else:
print("your name does not contain 's' ")
#1. 比較運算符
x != y x不等于y
x is y x和y是同一個對象
x is not y x和y是不同的對象
x in y x是y容器(例如序列)的成員
x not in y x不是y容器(例如序列)的成員
#2. is:同一性運算
#作用:is運算符看起來和==一樣,事實上卻不同
x = y = [1,2,3]
z = [1,2,3]
print(x==y==z) #the result : True
print(x is y) #the result : True
print(x is z) #the result : False
#3. in: 成員資格運算符
name = input('what is your name: ')
if 's' in name:
print('your name contains the letters "s" ')
else:
print("your name does not contain 's' ")
3、for循環
age = 66
count = 0
for i in range(3):
guess_age = int(input('age:'))
if guess_age == age:
print("right")
break
elif guess_age < age:
print("too small")
else:
print("too large")
else:
print("you have tried too many times")
age = 66
count = 0
for i in range(3):
guess_age = int(input('age:'))
if guess_age == age:
print("right")
break
elif guess_age < age:
print("too small")
else:
print("too large")
else:
print("you have tried too many times")
4、while循環
作用:一般來說循環會一直執行到條件為假,或到序列元素用完時,但是有些時候會提前終止一些循環
1)break :直接跳出循環
2)continue:跳出本次循環進行下一次循環
age = 66
count = 0
while count < 3:
guess_age = int(input('age:'))
if guess_age == age:
print("right")
break
elif guess_age < age:
print("too small")
else:
print("too large")
count += 1
else:
print("you have tried too many times")
age = 66
count = 0
while count < 3:
guess_age = int(input('age:'))
if guess_age == age:
print("right")
break
elif guess_age < age:
print("too small")
else:
print("too large")
count += 1
else:
print("you have tried too many times")
1.2 讀寫文件
1、open函數用來打開文件
1.open(name[, mode[, buffering]]) ?打開文件可傳的參數
1. open函數使用一個文件名作為唯一的強制參數,然后返回一個文件對象。
2. 模式(mode)和緩沖(buffering)參數都是可選的
2.?打開文件的模式有
? r,只讀模式(默認)。
? w,只寫模式。【不可讀;不存在則創建;存在則刪除內容;】
? a,追加模式。【可讀; 不存在則創建;存在則只追加內容;】
注:??"+" 表示可以同時讀寫某個文件
? w,只寫模式。【不可讀;不存在則創建;存在則刪除內容;】
? w+,寫讀
??a+,同a
3、with語句
作用:將打開文件寫在with中當對文件操作完成后with語句會自動幫關閉文件,避免忘記寫f.close()
with open("data1.txt",'r',encoding = 'utf-8') as f:
for line in f:
print(line)
with open("data1.txt",'r',encoding = 'utf-8') as f:
for line in f:
print(line)
2、三種讀操作比較
1. readline()每次讀取一行,當前位置移到下一行
2.?readlines()讀取整個文件所有行,保存在一個列表(list)變量中,每行作為一個元素
3.?read(size)從文件當前位置起讀取size個字節,如果不加size會默認一次性讀取整個文件(適用于讀取小文件)
#1. read()一次讀取所有內容
'''aaa111
bbb222'''
f = open(r"data.txt")
print(f.read())
f.close()
#2. readline(),每次只讀取一行,光標下移
'''
0: aaa111
1: bbb222
'''
f = open(r"data.txt")
for i in range(2):
print(str(i) + ": " + f.readline(),)
#3. 一次讀取所有,每行作為列表的一個值
'''['aaa111\n', 'bbb222\n']'''
f = open(r"data.txt")
print(f.readlines())
#1. read()一次讀取所有內容
'''aaa111
bbb222'''
f = open(r"data.txt")
print(f.read())
f.close()
#2. readline(),每次只讀取一行,光標下移
'''
0: aaa111
1: bbb222
'''
f = open(r"data.txt")
for i in range(2):
print(str(i) + ": " + f.readline(),)
#3. 一次讀取所有,每行作為列表的一個值
'''['aaa111\n', 'bbb222\n']'''
f = open(r"data.txt")
print(f.readlines())
3、使用read()讀文件
1. read(n)讀取指定長度的文件
f = open(r"somefile.txt")
print(f.read(7)) # Welcome 先讀出 7 個字符
print(f.read(4)) #‘ to ‘ 接著上次讀出 4 個字符
f.close()
f = open(r"somefile.txt")
print(f.read(7)) # Welcome 先讀出 7 個字符
print(f.read(4)) #‘ to ‘ 接著上次讀出 4 個字符
f.close()
2.?seek(offset[, whence])? ? ? ? ? 隨機訪問
作用:從文件指定位置讀取或寫入
f = open(r"somefile.txt", "w")
f.write("01234567890123456789")
f.seek(5)
f.write("Hello, World!")
f.close()
f = open(r"somefile.txt")
print(f.read()) # 01234Hello, World!89
f = open(r"somefile.txt", "w")
f.write("01234567890123456789")
f.seek(5)
f.write("Hello, World!")
f.close()
f = open(r"somefile.txt")
print(f.read()) # 01234Hello, World!89
3. tell ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?返回當前讀取到文件的位置下標
f = open(r"somefile.txt")
f.read(1)
f.read(2)
print(f.tell()) # 3 3就是讀取到文件的第三個字符
f = open(r"somefile.txt")
f.read(1)
f.read(2)
print(f.tell()) # 3 3就是讀取到文件的第三個字符
4、readline()讀文件
作用:readline 的用法,速度是fileinput的3倍左右,每秒3-4萬行,好處是 一行行讀 ,不占內存,適合處理比較大的文件,比如超過內存大小的文件
f1 = open('test02.py','r')
f2 = open('test.txt','w')
while True:
line = f1.readline()
if not line:
break
f2.write(line)
f1.close()
f2.close()
f1 = open('test02.py','r')
f2 = open('test.txt','w')
while True:
line = f1.readline()
if not line:
break
f2.write(line)
f1.close()
f2.close()
5、readlines()讀文件
作用:readlines會把文件都讀入內存,速度大大增加,但是木有這么大內存,那就只能乖乖的用readline
f1=open("readline.txt","r")
for line in f1.readlines():
print(line)
f1=open("readline.txt","r")
for line in f1.readlines():
print(line)
6、將data1.txt中內容讀取并寫入到data2.txt中
f1 = open('data1.txt','r')
f2 = open('data2.txt','w')
for line in f1:
f2.write(line)
f1.close()
f2.close()
f1 = open('data1.txt','r')
f2 = open('data2.txt','w')
for line in f1:
f2.write(line)
f1.close()
f2.close()
7、使用eval()方法將文件讀取成字典
f = open('data1.txt')
f1 = (f.read())
data = eval(f1)
f.close()
print(data) # 運行結果: {'k2': 'v2', 'k3': 'v3', 'k1': 'v1'}
f = open('data1.txt')
f1 = (f.read())
data = eval(f1)
f.close()
print(data) # 運行結果: {'k2': 'v2', 'k3': 'v3', 'k1': 'v1'}
8、將文件內容讀取成列表
lock = []
f = open("password.txt")
for name in f.readlines():
lock.append(name.strip('\n'))
print(lock)
運行結果: ['aaa 111', 'bbb 222', 'ccc 333']
lock = []
f = open("password.txt")
for name in f.readlines():
lock.append(name.strip('\n'))
print(lock)
運行結果: ['aaa 111', 'bbb 222', 'ccc 333']
1.3 字符編碼
1、幾種常用編碼
1.?ASCII ? ? ? :不支持中文
2.?GBK??????? :是中國的中文字符,其包含了簡體中文和繁體中文的字符
3.?Unicode :萬國編碼(Unicode包含GBK)
1) Unicode 是為了解決傳統的字符編碼方案的局限而產生的,它為每種語言中的每個字符設定了統一并且唯一的二進制編碼
2) 規定雖有的字符和符號最少由 16 位來表示(2個字節),即:2 **16 = 65536
3)這里還有個問題:使用的字節增加了,那么造成的直接影響就是使用的空間就直接翻倍了
4.?Utf-8?????? :可變長碼, 是Unicode的擴展集
1)? UTF-8編碼:是對Unicode編碼的壓縮和優化,他不再使用最少使用2個字節,而是將所有的字符和符號進行分類
2)ascii碼中的內容用1個字節保存、歐洲的字符用2個字節保存,東亞的字符用3個字節保存...
3、python2與python3的幾個區別
1.??Python2默認 編碼方式為ASCII,Python3??默認編碼方式為UTF-8(是Unicode的擴展集)
2. ?python2中字符串有str和unicode兩種類型,python3?中字符串有str和字節(bytes)?兩種類型
3. ?python3中不再支持u中文的語法格式
4、str與字節碼(bytes): ?python3中字符串兩種類型
比如定義變量:?s = "人生苦短"
1. s是個字符串,python3中字符串本身存儲的就是字節碼
2. 如果這段代碼是在解釋器上輸入的,那么這個s的格式就是解釋器的編碼格式,對于windows的cmd而言,就是gbk。
3. 如果將段代碼是保存后才執行的,比如存儲為utf-8,那么在解釋器載入這段程序的時候,就會將s初始化為utf-8編碼。
4. bytes是一種比特流,它的存在形式是01010001110這種,但為了在ide環境中讓我們相對直觀的觀察,
它被表現成了b'\xe4\xb8\xad\xe6\x96\x87'這種形式
5、unicode與str?:??python2中字符串兩種類型
1. ?我們知道unicode是一種編碼標準,具體的實現標準可能是utf-8,utf-16,gbk ......
2. ?python 在內部使用兩個字節來存儲一個unicode,使用unicode對象而不是str的好處,就是unicode方便于跨平臺
6、python2和python3中編碼轉換
1. ?在python3中字符串默認是unicode所以不需要decode(),直接encode成想要轉換的編碼如gb2312
2. ?在python2中默認是ASCII編碼,必須先轉換成Unicode,Unicode 可以作為各種編碼的轉換的中轉站
7、python2和python3中字符編碼轉換舉例
1.?python2進行字符編碼轉換
#!/usr/bin/env python
#-*- coding:utf8 -*-
ss = '北京市'
#1、第一步先將utf8的字符串解碼成unicode: str ----> Unicode
#注:因為上面聲明了使用utf8所以這里必須制定用utf8格式才能decode解碼成Unicode
unicode_type = ss.decode('utf8') # 將utf8的str格式轉換成 unicode編碼
print type(unicode_type) # type變成:
#2、第二步將unicode轉換成gbk
gbk_type = unicode_type.encode('gbk') # 將'unicode' encode編碼成gbk格式的str
print gbk_type.decode('gbk') # 只有將gbk格式的str再deode 解碼成 unicode才能顯示 "北京市"
# print type( gbk_type.decode('gbk') ) # 能正常顯示的只有: 格式
#!/usr/bin/env python
#-*- coding:utf8 -*-
ss = '北京市'
#1、第一步先將utf8的字符串解碼成unicode: str ----> Unicode
#注:因為上面聲明了使用utf8所以這里必須制定用utf8格式才能decode解碼成Unicode
unicode_type = ss.decode('utf8') # 將utf8的str格式轉換成 unicode編碼
print type(unicode_type) # type變成:
#2、第二步將unicode轉換成gbk
gbk_type = unicode_type.encode('gbk') # 將'unicode' encode編碼成gbk格式的str
print gbk_type.decode('gbk') # 只有將gbk格式的str再deode 解碼成 unicode才能顯示 "北京市"
# print type( gbk_type.decode('gbk') ) # 能正常顯示的只有: 格式
>>> ss = '北京市'
>>> us = ss.decode('gbk') # 將gbk格式的str解碼成unicode
>>> us2 = us.encode('utf8') # 將unicode變成utf8格式的str
>>> us3 = us2.decode('utf8') # 由于cmd中默認是gbk所以要先顯示正常漢子,還需要解碼成Unicode
>>> print us3
北京市
>>> print type(us3)
>>> ss = '北京市'
>>> us = ss.decode('gbk') # 將gbk格式的str解碼成unicode
>>> us2 = us.encode('utf8') # 將unicode變成utf8格式的str
>>> us3 = us2.decode('utf8') # 由于cmd中默認是gbk所以要先顯示正常漢子,還需要解碼成Unicode
>>> print us3
北京市
>>> print type(us3)
2. python3進行字符編碼轉換
import sys
print(sys.getdefaultencoding()) #在python3中打印出默認字符編碼: utf-8
s = "你好"
print(s.encode("gbk")) #將Unicode轉換為gbk: b'\xc4\xe3\xba\xc3'
print(s.encode("utf-8")) #將Unicode轉換為utf-8: b'\xe4\xbd\xa0\xe5\xa5\xbd'
print(s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312"))
# 1. s.encode("utf-8") #將Unicode編碼為utf-8
# 2. s.encode("utf-8").decode("utf-8") #將utf-8解碼為Unicode在解碼時必須指定現在的字符編碼“utf-8”
# 3. s.encode("utf-8").decode("utf-8").encode("gb2312") #將Unicode編碼為”gb2312”
# 4. s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312") #將gb2312解碼為unicode
# 注1:encode("utf-8") encode作用是將Unicode編碼編碼為指定編碼(這里的utf-8是要編碼成什么)
# 注2:decode(“utf-8”) decode作用是將其他編碼轉化為Unicode編碼(這里的utf-8是指定現在是什么編碼)
import sys
print(sys.getdefaultencoding()) #在python3中打印出默認字符編碼: utf-8
s = "你好"
print(s.encode("gbk")) #將Unicode轉換為gbk: b'\xc4\xe3\xba\xc3'
print(s.encode("utf-8")) #將Unicode轉換為utf-8: b'\xe4\xbd\xa0\xe5\xa5\xbd'
print(s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312"))
# 1. s.encode("utf-8") #將Unicode編碼為utf-8
# 2. s.encode("utf-8").decode("utf-8") #將utf-8解碼為Unicode在解碼時必須指定現在的字符編碼“utf-8”
# 3. s.encode("utf-8").decode("utf-8").encode("gb2312") #將Unicode編碼為”gb2312”
# 4. s.encode("utf-8").decode("utf-8").encode("gb2312").decode("gb2312") #將gb2312解碼為unicode
# 注1:encode("utf-8") encode作用是將Unicode編碼編碼為指定編碼(這里的utf-8是要編碼成什么)
# 注2:decode(“utf-8”) decode作用是將其他編碼轉化為Unicode編碼(這里的utf-8是指定現在是什么編碼)
8、頂部的:# -*- coding: utf-8 -*-或者# coding: utf-8目前有三個作用
1. ?如果代碼中有中文注釋,就需要此聲明。
2. ?比較高級的編輯器會根據頭部聲明,將此作為代碼文件的格式。
3. ?程序會通過頭部聲明,解碼初始化 u"人生苦短",這樣的unicode對象,(所以頭部聲明和代碼的存儲格式要一致)。
9、python2 字符編碼使用原則
1. ?decode early:在輸入或者聲明字符串的時候,盡早地使用decode方法將字符串轉化成unicode編碼格式;
2. ?unicode everywhere:然后在程序內使用字符串的時候統一使用unicode格式進行處理,比如字符串拼接等操作
3. ?encode late:最后,在輸出字符串的時候(控制臺/網頁/文件),通過encode方法將字符串轉化為你所想要的編碼格式,比如utf-8等
10、UnicodeDecodeError 解決方法
reload(sys)
sys.setdefaultencoding('utf-8')
總結
以上是生活随笔為你收集整理的python 循环写文件_循环-读写文件-字符编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分布计算环境学习笔记4——Enterpr
- 下一篇: 清北毕业生5年来去向大数据:北大偏爱银行