【Python爬虫作业】习题13-17

一,作业内容

笨办法学Python习题13-17

二,作业代码

习题13 参数、解包和变量

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)

应该看到的结果

The script is called: C:/Users/PycharmProjects/python3_project/ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd

附加练习
1,给脚本3个以下的参数,会得到错误信息。

Traceback (most recent call last):
  File "C:/Users/PycharmProjects/python3_project/ex13.py", line 3, in <module>
    script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 3)

因为我们设置的参数变量包括脚本本身是4个:script, first, second, third, 如果除了脚本少于3个,程序会出错。
2,
接受更少参数的脚本

from sys import argv

script, first, second = argv

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

结果是

The script is called: C:/Users/PycharmProjects/python3_project/ex13-1.py
Your first variable is: apple
Your second variable is: orange

接受更多参数的脚本

from sys import argv

script, first, second, third, fourth, fifth = 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)
print('Your third variable is:', fourth)
print('Your third variable is:', fifth)

结果是

The script is called: C:/Users/PycharmProjects/python3_project/ex13-2.py
Your first variable is: Learn
Your second variable is: Python
Your third variable is: the
Your third variable is: Hard
Your third variable is: Way

3,将input和argv一起使用

from sys import argv

script, first, second = argv
first = input("> ")
second = input("> ")

print('The script is called:', script)
print('Your first variable is: %s' % first)
print('Your second variable is: %s' % second)
> Hello
> Baby
The script is called: C:/Users/PycharmProjects/python3_project/ex13-3.py
Your first variable is: Hello
Your second variable is: Baby

4, 模块需要导入(import),比如from sys import argv,把 sys模块导入进来,模块也称为库(library)。

习题13 心得体会:
一,在命令行中运行带参数的脚本时,可以直接把脚本名及其参数列上,如 python ex13.py first 2nd 3rd, 但是在Pycharm中运行脚本,需要设置脚本的参数,在Run--Edit configurations... 中设置Script parameters.
二,参数是变量,与input函数一起使用时,在print模块需要引用变量, %s % first

习题14 提示和传递

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 likeing me. '
      '\nYou live in %r. Not sure where that is. '
      '\nAnd you have a %r computer. Nice.' %(likes, lives, computer))

结果

Hi Faye, I'm the C:/Users/PycharmProjects/python3_project/ex14.py script.
I'd like to ask you a few questions.
Do you like me Faye
> Yes
Where do you live Faye?
> Beijing
What kind of computer do you have?
> Lenovo
Alright, so you said 'Yes' about likeing me. 
You live in 'Beijing'. Not sure where that is. 
And you have a 'Lenovo' computer. Nice.

附加练习
2,3题

from sys import argv

script, user_name, one_person  = 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('What a nice day, %s' % one_person)
one_person = 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 likeing me. '
      '\nYou live in %r. Not sure where that is. '
      '\nAnd you have a %r computer. Nice.'
      '\nWhat a nice day, %s'%(likes, lives, computer, one_person))

习题15 读取文件

from sys import argv  #从模块sys导入参数argv

script, filename = argv # 参数包含script和filename

txt = open(filename) #txt方法是打开文件

print('Here\'s your file %r:' % filename) #打印“Here's your file ‘ex15_sample.txt’”
print(txt.read()) #print txt的文件内容

print('Type the filename again:') #打印'Type the filename again:'
file_again = input("> ") # 输入文件名

txt_again = open(file_again) # 打开文件名

print(txt_again.read()) # 打印所输入文件名的文件内容

应该看到的结果

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.

附加练习
5,用自定义输入的方法获取文件名称更好,这样,这段代码可以重复使用,打开很多文件。
6

习题16 读写文件

from sys import argv #从sys模块引入参数变量argv

script, filename = argv # 参数解包成script 和 filename

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('line 1: ')# 输入第一行信息
line2 = input('line 2: ') # 输入第二行信息
line3 = input('line 3: ')  # 输入第三行信息

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() # 关闭目标文件

应该看到的结果

We're going to erase 'test.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.
line 1: Mary had a little lamb
line 2: It's fleece was white as snow
line 3: It was also tasty
I'm going to write these to the file.
And finally, we close it.

附加练习
3,

from sys import argv #从sys模块引入参数变量argv

script, filename = argv # 参数解包成script 和 filename

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('I\'m going to write these to the file.') # 打印

target.write('Mary had a little lamb \nIt\'s fleece was white as snow \nIt was also tasty')


print('And finally, we close it.')
target.close() # 关闭目标文件

4,给open赋予一个‘w’参数,打开文件并且是写入模式。

习题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 coud 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, 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()

应该看到的结果

Copying from cat.txt to test.txt
The input file is 25 bytes long
Does the output file exist? True
Ready, hit RETURN to continue, CTRL-C to abort.
yes
Alright, all done

附加练习
6,为什么需要在代码中写output.close()? 因为open函数的返回值是一个文件句柄, 这个句柄从操作系统托付给你的Python程序。一旦处理完文件,要归还这个文件句柄,只有这样程序不会超出一次能打开的文件句柄的数量上限。

心得:习题17的附加练习的题目还不理解,需要继续学习。

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

推荐阅读更多精彩内容

  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,215评论 9 467
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,633评论 18 139
  • 习题0:准备工作 总有一天你会听到有程序员建议你使用 Mac OSX 或者 Linux。如果他喜欢字体美观,他会告...
    KennyP0618阅读 2,214评论 0 1
  • 今天,又是11月,倒数第二天了。 这是我出来毕业实习以后第一次,跳出这个地方,第一次走在大街上的时候,那种孤独,是...
    熊芳菲阅读 156评论 0 0
  • 菲利普曲线的移动。短期菲利普曲线是斜向下的。因为失业率高,那么政府在减少货币供给;长期菲利普曲线的失业率就是一根线...
    sunflower_d66c阅读 122评论 0 0