如何解决非常基本的Python问题字符串,格式和转义符
|| 我开始通过在线指南学习Python,而我刚做了一个练习,需要我编写以下脚本: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.\"
raw_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 = raw_input(\"line 1: \")
line2 = raw_input(\"line 2: \")
line3 = raw_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()
我运行正常,但随后指南说:
\“此文件中的重复太多。使用字符串,格式和转义符仅通过一个target.write()命令而不是6来打印第1行,第2行和第3行。”
我不确定该怎么做。有人可以帮忙吗?谢谢!
解决方法
该指南建议创建一个字符串并将其写出,而不是六次调用
write()
,这似乎是个不错的建议。
您有三个选择。
您可以像这样将字符串并置在一起:
line1 + \"\\n\" + line2 + \"\\n\" + line3 + \"\\n\"
或像这样:
\"\\n\".join(line1,line2,line3) + \"\\n\"
您可以使用旧的字符串格式来做到这一点:
\"%s\\n%s\\n%s\\n\" % (line1,line3)
最后,您可以使用Python 3中使用的较新的字符串格式,也可以从Python 2.6中使用:
\"{0}\\n{1}\\n{2}\\n\".format(line1,line3)
我建议使用最后一种方法,因为当您掌握它时,它是最强大的,它将为您提供:
target.write(\"{0}\\n{1}\\n{2}\\n\".format(line1,line3))
, 怎么样
target.write(\'%s \\n %s \\n %s\' % (line1,line3))
, 我认为他们希望您使用字符串连接:
target.write(line1 + \"\\n\" + line2 + \"\\n\" + line3 + \"\\n\")
可读性差得多,但您只有一个target.write()
命令
, 这需要两行。
它将您要打印的行放在一个变量中,以使其更具可读性
lb = \"\\n\"
allOnOne= line1 + lb + line2 + lb+ line3 + lb
target.write(allOnOne)
, 我目前正在按照同一门课程学习,发现的解决方案与使用的忍者壁虎相似,不同之处在于,我只使用了到目前为止在课程中所学到的东西。我的看起来像这样:
from sys import argv
script,filename = argv
print \"We\'re going to erase %s.\" % filename
print \"If you don\'t want that,hit CTRL-C (^C).\"
print \"If you do want that,hit RETURN.\"
raw_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.\"
lines = [raw_input(\"Lines %r :\" % i) for i in range(1,4)]
for line in lines:
target.write(line + \"\\n\")
print \"And finally,we close it.\"
target.close()
我花了一点时间移动括号并弄清楚格式化程序和循环的放置位置,但是一旦找到它,对我来说就很有意义了。要注意的一件事是我的第一次尝试:
for i in range(1,4):
lines = raw_input(\"Line %r :\" % i)
最初在运行脚本时似乎可以工作,但是在查看目标文件时,它仅将最后一行(第3行)写入文件。我仍然不清楚为什么会这样。
, 我也是第一次参加本课程,并且想知道同一件事,这就是我想出的一切,并且使它可以毫无问题地工作。我仍在学习此方法,因此,如果这是不好的形式,请告诉我。这就是我要为我工作的。,
target.write(\"%s \\n%s \\n%s\" % (line1,line3))
, 出于该特定指南中“该特定研究钻探”的问题/问题的目的,我相信作者希望...
target.write(\"%s\\n%s\\n%s\\n\" % (line1,line3))
尽管,戴夫·韦伯(Dave Webb)当然也因透彻的学习和教育价值而获得了许多布朗尼点。
, 坏
原始代码是重复的,粘贴代码很危险(为什么“复制和粘贴”代码很危险?):
print \"Now I\'m going to ask you for three lines.\"
line1 = raw_input(\"line 1: \")
line2 = raw_input(\"line 2: \")
line3 = raw_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\")
好
短得多,只需更改一个字符即可将其更改为4行以上:
print \"Now I\'m going to ask you for three lines.\"
lines = [raw_input(\"line {i}: \".format(i=i)) for i in range(1,4)]
print \"I\'m going to write these to the file.\"
for line in lines:
target.write(line+\'\\n\')
, 我认为目的是让学生利用上一课中讲授的内容并得出以下解决方案:
print \"Now I\'m going to ask you for three lines.\"
line1 = raw_input(\"line 1: \")
line2 = raw_input(\"line 2: \")
line3 = raw_input(\"line 3: \")
print \"I\'m going to write these to the file.\"
target.write(line1 + \'\\n\' + line2 + \'\\n\' + line3 + \'\\n\')
print \"And finally,we close it.\"
target.close()
, 这个怎么样?我使用了for循环。
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 want that,hit RETURN.\")
input(\"?\")
print(\"Opening the file...\")
target = open(filename,\'w\')
print(\"Truncating the file. Goodbye!\")
target.truncate()
print(\"Now I am 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.\")
for a in (line1,line3):
target.write(\"\\n\")
target.close()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。