【Python爬蟲】03作業(yè)

一、作業(yè)內(nèi)容:習(xí)題13-17解包、參數(shù)、文件讀寫
習(xí)題13.
使用import語句用于導(dǎo)入其他模塊代碼,被引用模塊中的代碼和變量對(duì)該程序可見。

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)

結(jié)果:

C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex13.py first, second, third
The script is called: E:/pyproject/ex13.py
Your first variable is: first,
Your second variable is: second,
Your third variable is: third

Process finished with exit code 0

值得注意:argv解包,在pycharm中需要通過Edit Configurations中的script parameter中輸入被解包的內(nèi)容。

習(xí)題14.
使用input來通過鍵盤輸入問題,使用>來作為提示符,將用戶提示符設(shè)計(jì)為變量prompt,這樣無需在每次用到input時(shí)重復(fù)輸入提示符。

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 live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer))

結(jié)果:

C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex14.py user_name
Hi user_name, I'm the E:/pyproject/ex14.py script.
I'd like to ask you a few questions.
Do you like me user_name?
>yes
WHere do you live user_name
>America
What kind of computer do you have?
>Tandy

Alright, so you said 'yes' about liking me.
You live in 'America'. Not sure where that is.
And you have a 'Tandy' computer. Nice.


Process finished with exit code 0

如果將prompt變量改成不同的內(nèi)容,每次將出現(xiàn)新賦予的變量符。"""用于定義多行字符串。
習(xí)題15
讀取文件,這里涉及兩個(gè)文件,一個(gè)是ex15.py,另外一個(gè)是ex15_sample.txt,第二個(gè)文件是供腳本讀取的文本文件。
file_name='test.txt'
with open(file_name,'w',encoding='utf-8') as file:
file.write('text.txt')
file.write('\n')
file.write('test1.txt')
file.write('\n')
在這幾習(xí)題中要弄懂如何讀取文件,采用open(文件名)命令打開你要讀取的文件,并用txt.read()來執(zhí)行讀取。

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:\ProgramData\Anaconda3\python.exe E:/pyproject/ex15.py ex17.txt
Here's your file 'ex17.txt':
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("Ready, hit RETURN to continue, CTR_C to abort.")

input()

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

print("Alright, all done.")

out_file.close()
in_file.close()
Type the filename again:
>ex17.txt
<built-in method read of _io.TextIOWrapper object at 0x00000000003BAB40>

Process finished with exit code 0

注意:from sys import argv中的sys 是一個(gè)代碼庫,這句話的意思是從庫里取出argv這個(gè)功能。
習(xí)題16
讀取文件,記住各種文件的相關(guān)命令。
close()關(guān)閉文件
read()讀取文件內(nèi)容。
readline()讀取文件的每一行。
truncate()清空文件
write(stuff)將stuff寫入文件。

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 RETURN.")

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

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

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

line1 = input("line1:")
line2 = input("line2:")
line3 = input("line3:")

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

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print("And finally, we close it.")
target.close()

結(jié)果:

C:\ProgramData\Anaconda3\python.exe E:/pyproject/ex16.py ex17.txt
We're going to erase 'ex17.txt'.
If you don't want that, hit CTRL-C (^C).
If you do want that, hit RETURN.
?
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines.
line1:To all the people out there.
line2:I say I don't like my hair.
line3:I need to shave it off.
I'm going to write these to the file.
And finally, we close it.

Process finished with exit code 0

習(xí)題17
將一個(gè)文件的內(nèi)容拷貝到另外一個(gè)文件中。

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("Does the output file exist? %r" % exists(to_file))
print("Ready, hit RETURN to continue, CTR_C to abort.")

input()

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

print("Alright, all done.")

out_file.close()
in_file.close()

在運(yùn)行上面代碼時(shí)遇到了很大的困難,這一章節(jié)理解起來很吃力,不爽。在argv的解包中需要將列表內(nèi)容逐一呈現(xiàn)出來。用pycharm 涉及到先讀取,讀取之后再改變其中copy的變量,在寫入新變量。
inputFile = open("inputFile.txt", "r")
print("Name of the input file:", inputFile.name;)

outputFile = open(""onputFile.txt", "a"");
print("Name of the output file:", outputFile.name;)

allLines = inputFile.readlines();
for eachLine in allLines:
print("current line content:%s" % (eachline);)
outputFile.write(eachLine);
inputFile.close()
outputFile.close()

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

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

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