【Python爬虫】-第二周练习- 习题13-17

ex13

代码

from sys import argv#从系统中输入参数

script, first, second, third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

总结:

后来yaung下午截图,给我讲解14,我又重新做了13,做出来了,感谢!

13
ex14
#ex14 提示换个传递的代码练习

from sys import argv

script, user_name = argv
prompt = '> '

print("Hi %s, I'm the %s script." % (user_name, script))
print("I'd like to ask you a few questions.")
print("Do you like me %s?" % user_name)
likes = input(prompt)

print("Where do you live %s?" % user_name)
lives = input(prompt)

print("What kind of computer do you have?")
computer = input(prompt)

print("""
Alright, so you said %r about liking me.
You liking in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer))

运行结果

"C:\Program Files\Python36\python.exe" "D:/小克学习/python/项目/week two/ex14.py" Angle
Hi Angle, I'm the D:/小克学习/python/项目/week two/ex14.py script.
I'd like to ask you a few questions.
Do you like me Angle?
> like
Where do you live Angle?
> China
What kind of computer do you have?
> apple

Alright, so you said 'like' about liking me.
You liking in 'China'. Not sure where that is.
And you have a 'apple' computer. Nice.


Process finished with exit code 0

这几章的联系,总是有很多困难,感谢老师的指导。

总结:

1.在python 3. x 版本中,input()的输入格式发生了变化,这个得记住。
2.input是操作键盘的,我们得输入,才执行下面代码(左侧的红色区域就是提示)

ex15

# ex15 读取文件的练习

from sys import argv#解包,和前面的一样

script, filename = argv

txt = open(filename)

print("Here's your file %r:" % filename)
print(txt.read ())

print("Type the filename again:")
file_again = input()

txt_again = open(file_again)
print(txt_again.read())

运行结果

"C:\Program Files\Python36\python.exe" "D:/小克学习/python/项目/week two/ex15.py" ex15_sample.txt
Here's your file 'ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Type the filename again:
ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Process finished with exit code 0

总结:

1.这次熟悉了很多,虽然还是有运行失误,但是自己能按照逻辑进行修改了。

2.txt文件,需要放在同一个路径中去。

python:open/文件操作

open/文件操作f=open('/tmp/hello','w')#open(路径+文件名,读写模式)#读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式如:'rb','wb','r+b'等等
读写模式的类型有:
rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)w 以写方式打开,a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)r+ 以读写模式打开w+ 以读写模式打开 (参见 w )a+ 以读写模式打开 (参见 a )rb 以二进制读模式打开wb 以二进制写模式打开 (参见 w )ab 以二进制追加模式打开 (参见 a )rb+ 以二进制读写模式打开 (参见 r+ )wb+ 以二进制读写模式打开 (参见 w+ )ab+ 以二进制读写模式打开 (参见 a+ )
注意:
1、使用'W',文件若存在,首先要清空,然后(重新)创建,
2、使用'a'模式 ,把所有要写入文件的数据都追加到文件的末尾,即使你使用了seek()指向文件的其他地方,如果文件不存在,将自动被创建。

ex16

from sys import argv

script, filename = argv

print("We're going to erase %r." % filename)
print("If you don't want that, hit CTRL_C (^C).")
print("If you do want that, hit RETURE.")
input("?")

print("Opening the file...")
target = open(filename, 'w')

print("Truncating the file. Goodbey!")
target.truncate()

print("Now I'm going to ask you for three lines.")

line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")

print("I'm going to write these to the file.")

target.write("%s \n%s \n%s" % (line1, line2, line3))

print("And finally, we close it.")
target.close()

运行结果

"C:\Program Files\Python36\python.exe" "D:/小克学习/python/项目/week two/ex16.py" ex15_sample.txt
We're going to erase 'ex15_sample.txt'.
If you don't want that, hit CTRL_C (^C).
If you do want that, hit RETURE.
?
Opening the file...
Truncating the file. Goodbey!
Now I'm going to ask you for three lines.
line 1: 
line 2: 
line 3: 
I'm going to write these to the file.
And finally, we close it.

Process finished with exit code 0

总结:

在代码中的,重复地方,直接用了新的格式

target.write("%s \n%s \n%s" % (line1, line2, line3))

这种写法简洁很多。

想问下老师为什么总是在print下面写 target.write(),
有模糊的感觉,但是抓不住。

ex17

#ex17的练习代码-更多的文件操作

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print("Copying from %s to %s" % (from_file, to_file))

#We could do these two on one line too, how?
in_file = open(from_file)
indata = in_file.read()

print("The input file is %d bytes long" % len(indata))

print("Dose the output file exist? %r" % exists(to_file))
print("Ready, hit RETURE to continue, CTRL_C to abort.")
input()

out_file = open(to_file, 'w')
out_file.write(indata)

print("Alright, all done. ")

out_file.close()
in_file.close()

运行结果

"C:\Program Files\Python36\python.exe" "D:/小克学习/python/项目/week two/ex17.py" test.txt copied.txt
Copying from test.txt to copied.txt
The input file is 0 bytes long
Dose the output file exist? True
Ready, hit RETURE to continue, CTRL_C to abort.

cat copied.tx
Alright, all done. 

Process finished with exit code 0

该命令对于任何文件都应该是有效的。试试操作一些别的文件看看结果。不过小心别把你的重要文件给弄坏了。

Warning你看到我用 cat 这个命令了吧?它只能在 Linux 和 OSX 下面使用,使用 Windows 的就只好跟你说声抱歉了。

做完之后发现不对劲,出现了这句话

总结:

越写越懵逼,最好需要在晚上积累沉淀下。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,091评论 9 467
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,127评论 2 34
  • 基于字符读写 最基本的文件操作当然就是在文件中读写数据。这也是很容易掌握的。现在打开一个文件以进行写操作: 1. ...
    IT小白1002阅读 1,012评论 0 1
  • raw_input 和 argv 是操作文件的基础,不熟悉的需要回到上节继续练习。 一、代码分析 #coding:...
    大猫黄阅读 1,310评论 2 4
  • 哎,寒假过的太爽果然回来进入不了状态。还好DC历史性大雪把寒假强行拉长了点。想了想2016前半年真的会过得很快。 ...
    嘻笑到日落阅读 114评论 2 0