非常基本的Python问题(字符串,格式和转义)

时间:2011-06-18 05:52:13

标签: python string

我开始通过在线指南学习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来打印line1,line2和line3。”

我不知道该怎么做。有人可以帮忙吗?谢谢!

10 个答案:

答案 0 :(得分:16)

该指南建议创建一个字符串并将其写出来,而不是六次调用write(),这似乎是一个好建议。

你有三种选择。

您可以将字符串连接在一起,如下所示:

line1 + "\n" + line2 + "\n" + line3 + "\n"

或者像这样:

"\n".join(line1,line2,line3) + "\n"

您可以使用旧string formatting来执行此操作:

"%s\n%s\n%s\n" % (line1,line2,line3)

最后,您可以使用Python 3中使用的newer string formatting以及Python 2.6中提供的内容:

"{0}\n{1}\n{2}\n".format(line1,line2,line3)

我建议使用最后一种方法,因为它是最强大的,当你掌握它时,它会给你:

target.write("{0}\n{1}\n{2}\n".format(line1,line2,line3))

答案 1 :(得分:5)

怎么样

target.write('%s \n %s \n %s' % (line1,line2,line3))

答案 2 :(得分:1)

我认为他们希望你使用字符串连接:

target.write(line1 + "\n" + line2 + "\n" + line3 + "\n")

可读性差得多,但只有一个target.write()命令

答案 3 :(得分:1)

这有两行。 它将您要打印的行放在一个变量中,以便它更具可读性

lb = "\n"
allOnOne= line1 + lb + line2 + lb+ line3 + lb 
target.write(allOnOne) 

答案 4 :(得分:1)

我目前正在遵循相同的课程,我找到的解决方案类似于使用的ninjagecko,除了我在课程中仅使用了你在这一点上教过的东西。我看起来像这样:

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)

当您运行脚本时,似乎首先工作,但是当您查看目标文件时,它只会将最后一行(line3)写入文件。我还不完全清楚为什么会这样。

答案 5 :(得分:1)

我刚刚第一次把这个课程当作自己,并且想知道同样的事情,这就是我想出来并让它无故障地工作。我还在学习这个,所以如果这是不好的形式让我知道。这就是我为我工作的原因。

target.write("%s \n%s \n%s" % (line1,line2,line3))

答案 6 :(得分:1)

对于该特定指南中“特定研究训练”的问题/问题,我相信作者希望......

target.write("%s\n%s\n%s\n" % (line1, line2, line3))

尽管如此,Dave Webb在获得教育价值方面肯定会获得许多空白点。

答案 7 :(得分:0)

原始代码是重复的,复制粘贴代码很危险(Why is "copy and paste" of code dangerous?):

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

答案 8 :(得分:0)

我认为目的是让学生利用以前课程中教授的内容,并作为解决方案达到以下目的:

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

答案 9 :(得分:0)

这个怎么样?我使用了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, line2, line3):
    target.write("\n")

target.close()