到目前为止已经了解了如何处理数据,显示数据那么如何向程序传入数据
创建文件夹HeadFirstPython/chapter3 下载演示文件
wget http://python.itcarlow.ie/chapter3/sketch.txt
内容如下
Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!
(pause)
Man: It's just contradiction!
Other Man: No it isn't!
Man: It IS!
Other Man: It is NOT!
Man: You just contradicted me!
Other Man: No I didn't!
Man: You DID!
Other Man: No no no!
Man: You did just then!
Other Man: Nonsense!
Man: (exasperated) Oh, this is futile!!
(pause)
Other Man: No it isn't!
Man: Yes it is!
从标准库导入os
>>> import os
>>> os.getcwd()
'/server/scripts/HeadFirstPython/chapter3' #显示当前目录
>>> data = open('sketch.txt') #打开文件,将文件赋至一个名为data的文件对象
>>> print(data.readline(),) #使用readline() 方法从文件获取一个数据行,使用print打印
Man: Is this the right room for an argument?
>>> print(data.readline(),)
("Other Man: I've told you once.\n",) #再读取一行
>>> data.seek(0) #使用seek()方法返回文件其实位置
>>> print(data.readline(),)
('Man: Is this the right room for an argument?\n',)
>>> for each_line in data: #使用标准迭代输出数据,这里输出省略
... print each_line
data.close() #最后不要忘记关闭
>>> data = open('sketch.txt')
>>> for each_line in data:
... (role,line_spoken) = each_line.split(':')
... print (role,)
... print (line_spoken,)
前面后面出现了ValueError错误
注意到处错误的哪一行有两个:号所以split把这一行分了3个部分,抱怨值过多了
help(each_line.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.
可以在分割的时候指定参数分割为两行即可
为了方便建立一个脚本
sketch.py
data = open('sketch.txt')
for each_line in data:
(role,line_spoken) = each_line.split(':',1)
print (role,)
print (line_spoken,)
还是会出错
ValueError: need more than 1 value to unpack
检查数据发现有几行是没有:的所以出错了
要处理大量意外情况,最好的办法是增加额外的逻辑来进行处理,如果有更多担心的问题就需要更多的代码
或者可以允许这些错误的发生,然后在错误发生的时候处理相应错误
那一种方法更好呢
使用find方法可以让find找到一个字符串中的子串
each_line = "I am liuyueming"
>>> each_line.find('a')
2
>>> each_line.find('w')
-1
如果找到返回出现在第几位,否则返回-1
修改代码
data = open('sketch.txt')
for each_line in data:
if not each_line.find(':') == -1:
(role,line_spoken) = each_line.split(':',1)
print (role,)
print ('said:',)
print (line_spoken,)
data.close()
输出正确了没有错误提示,但是假如文件格式改变了还会有其他问题,又需要修改代码了
在Python里面运行错误叫异常,可以通过try/expect机制跳过异常
data = open('sketch.txt')
for each_line in data:
#if not each_line.find(':') == -1:
try:
(role,line_spoken) = each_line.split(':',1)
print (role,)
print ('said:',)
print (line_spoken,)
except:
pass
data.close()
PS:pass代表什么也不做,try和expect之间就是要保护的代码,假如需要忽略特定错误比如ValueError可以把这个参数加在expect后面