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 "", line 2, in
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 "", line 2, in
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!')