【Python爬蟲】-第二周練習- 習題13-17

ex13

代碼

from sys import argv#從系統(tǒng)中輸入?yún)?shù)

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)

總結(jié):

后來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))

運行結(jié)果

"C:\Program Files\Python36\python.exe" "D:/小克學(xué)習/python/項目/week two/ex14.py" Angle
Hi Angle, I'm the D:/小克學(xué)習/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

這幾章的聯(lián)系,總是有很多困難,感謝老師的指導(dǎo)。

總結(jié):

1.在python 3. x 版本中,input()的輸入格式發(fā)生了變化,這個得記住。
2.input是操作鍵盤的,我們得輸入,才執(zhí)行下面代碼(左側(cè)的紅色區(qū)域就是提示)

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())

運行結(jié)果

"C:\Program Files\Python36\python.exe" "D:/小克學(xué)習/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

總結(jié):

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 開始, 必要時創(chuàng)建新文件)r+ 以讀寫模式打開w+ 以讀寫模式打開 (參見 w )a+ 以讀寫模式打開 (參見 a )rb 以二進制讀模式打開wb 以二進制寫模式打開 (參見 w )ab 以二進制追加模式打開 (參見 a )rb+ 以二進制讀寫模式打開 (參見 r+ )wb+ 以二進制讀寫模式打開 (參見 w+ )ab+ 以二進制讀寫模式打開 (參見 a+ )
注意:
1、使用'W',文件若存在,首先要清空,然后(重新)創(chuàng)建,
2、使用'a'模式 ,把所有要寫入文件的數(shù)據(jù)都追加到文件的末尾,即使你使用了seek()指向文件的其他地方,如果文件不存在,將自動被創(chuàng)建。

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()

運行結(jié)果

"C:\Program Files\Python36\python.exe" "D:/小克學(xué)習/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

總結(jié):

在代碼中的,重復(fù)地方,直接用了新的格式

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()

運行結(jié)果

"C:\Program Files\Python36\python.exe" "D:/小克學(xué)習/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

該命令對于任何文件都應(yīng)該是有效的。試試操作一些別的文件看看結(jié)果。不過小心別把你的重要文件給弄壞了。

Warning你看到我用 cat 這個命令了吧?它只能在 Linux 和 OSX 下面使用,使用 Windows 的就只好跟你說聲抱歉了。

做完之后發(fā)現(xiàn)不對勁,出現(xiàn)了這句話

總結(jié):

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容