1.介紹基礎文件,輸入,輸出
open()
打開文件,一次傳入一行數據,可以結合for循環和readline()來使用
close()
用來關閉open打開的文件
the_file = open('sketch.txt')
the_file.close()
例子:
>>> data = open('/root/python_test/site_list.txt')
>>> print(data.readline())
www.godblessyuan.com
一些基礎的目錄管理函數
>>> import os
>>> os.getcwd() #獲取當前目錄
'/root'
>>> os.chdir('/root/python_test') #切換目錄
>>> os.getcwd()
'/root/python_test'
>>>
2.需要對數據做一些處理,例如像分隔它,以冒號為分界符
>>> data = open('/root/python_test/headfirstpython/sketch.txt')
>>> for each_line in data:
... (role,line_spoken) = each_line.split(':') #這里使用idel時候,需要注意的是代碼之間的縮進
... print role
... print line_spoken
...
Other Man
Anyway, I did.
Man
You most certainly did not!
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: too many values to unpack
遇到報錯了,報錯意思大概是太多值導致沒有被處理,檢查發現是因為有些數據是超過一個冒號的,所以這些數據會出錯,因為split()處理不了,但是檢查了split函數的使用說明,發現是可以支持這種情況的,
>>> help(each_line.split)
Help on built-in function split:
split(...)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.
(END)
1.檢查方式可以參考上面的方法。
2.支持這種情況的參數是一個叫maxsplit的參數,如果有maxsplit的話,那么至多是maxsplit的數量以上的分界符才會被處理,這樣就很好的避免太多分界符的情況了。
如這樣:
data = open('sketch.txt')
for each_line in data:
(role, line_spoken) = each_line.split(':', 1)
print role
print line_spoken
data.close()
不過,即使加了參數,還是遇到報錯了
Man
Oh no you didn't!
Other Man
Oh yes I did!
Man
Oh look, this isn't an argument!
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: need more than 1 value to unp
這里是說需要超過一個值去處理,查看了數據,發現是有些數據沒有冒號導致程序處理失敗。
3.增加邏輯來處理
首先通過觀察find()方法對于不同的數據返回的值是不同的。
>>> each_line = 'iiiii'
>>> each_line.find(':')
-1
>>> each_line = 'iiiii:'
>>> each_line.find(':')
5
>>>
然后可以使用的邏輯有2種,一種是if判斷,另外一種是try:expoet
try:
你的代碼(可能會導致運行錯誤的)
except:
錯誤回復代碼
這種方式的機制是通過捕獲某代碼的錯誤,然后執行響應的修復代碼,例子:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':', 1)
print role
print line_spoken
except:
pass
data.close()
如果
(role, line_spoken) = each_line.split(':', 1)
print role
print line_spoken
這里有其中一句代碼是執行失敗的,都會轉到pass里面去,pass代表空語句,或者null,什么也不做。
或者就是使用最簡單的if判斷
for each_line in data:
if not each_line.find(':') == -1: #not關鍵字是將一個條件取反
(role, line_spoken) = each_line.split(':', 1)
print(role, end='')
print(' said: ', end='')
print(line_spoken, end='')
data.close()
不過需要注意的是,像下面這種多重try:except的代碼是很容易影響到我們判斷那一部分代碼才是真正有問題的代碼,因為無論里面和外面的try出錯了,都會返回 print('The datafile is missing!'),這樣就不能判斷是那部分代碼有問題了。
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':', 1)
print role
print line_spoken
except:
pass
data.close()
except:
print('The datafile is missing!')
所以需要加一些標記,標識(ValueError-數據不符合期望的格式時會出現,IOError-數據無法正常訪問時會出現。)
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':')
print role
print line_spoken
except ValueError:
pass
data.close()
except IOError:
print('The datafile is missing!')
http://www.godblessyuan.com/2015/04/20/head_first_python_chapter_3_learning/