【Python爬虫】03作业

一、作业内容:习题13-17解包、参数、文件读写
习题13.
使用import语句用于导入其他模块代码,被引用模块中的代码和变量对该程序可见。

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)

结果:

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中输入被解包的内容。

习题14.
使用input来通过键盘输入问题,使用>来作为提示符,将用户提示符设计为变量prompt,这样无需在每次用到input时重复输入提示符。

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

结果:

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变量改成不同的内容,每次将出现新赋予的变量符。"""用于定义多行字符串。
习题15
读取文件,这里涉及两个文件,一个是ex15.py,另外一个是ex15_sample.txt,第二个文件是供脚本读取的文本文件。
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')
在这几习题中要弄懂如何读取文件,采用open(文件名)命令打开你要读取的文件,并用txt.read()来执行读取。

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:\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


Process finished with exit code 0

注意:from sys import argv中的sys 是一个代码库,这句话的意思是从库里取出argv这个功能。
习题16
读取文件,记住各种文件的相关命令。
close()关闭文件
read()读取文件内容。
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()

结果:

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

习题17
将一个文件的内容拷贝到另外一个文件中。

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

在运行上面代码时遇到了很大的困难,这一章节理解起来很吃力,不爽。在argv的解包中需要将列表内容逐一呈现出来。用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()

你可能感兴趣的:(【Python爬虫】03作业)