【问题标题】:Total Python Noob: why doesn't this work?Total Python Noob:为什么这不起作用?
【发布时间】:2011-11-06 04:34:45
【问题描述】:

我正在学习 Python the Hard Way,并尝试理解它,而不是一锤定音。我被困在练习 16 上,正如这里已经讨论过的那样:

Very basic Python question (strings, formats and escapes)

但我仍在试图弄清楚为什么这种方法不起作用:

from sys import argv

script, filename = argv


print "Attempting to open the file now." 
print open(filename).read()

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-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." 

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

target.write("the ending line")

print "And finally, we close it." 
target.close()

我已经为换行建立了一个值,并在 target.write 命令中使用 %s 调用 line1、line2 和 linebreak 值。读取时不应该解析为“line1 \n line2 \n line3 \n”吗?

这可能相当于被一个孩子问是什么让天空保持向上或什么的,我为有点厚实道歉。谢谢!

【问题讨论】:

  • 你能详细说明“不起作用”吗?你有例外吗?如果有,请提供。如果不是,请说明您的预期和实际收到的内容。
  • 如果有机会我可以猜到 - 当您重新打开文件以验证其内容时,您使用的编辑器是否可能无法将 \n 识别为换行,它实际上是在寻找\r\n(回车+换行)?例如,这在 Windows 上的记事本中很常见。
  • 道歉——当它到达脚本的 target.write 部分时,我收到“TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'”。

标签: python string line new-operator


【解决方案1】:

假设你得到了

TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

你需要的是

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

也就是说,您需要对字符串使用% 运算符,而不是target.write() 的结果。如果您意识到target.write() 返回None(其类型为NoneType),则错误消息可能对您更有意义。

【讨论】:

    【解决方案2】:
    target.write("%s %s %s %s %s %s") % (line1, linebreak, line2, linebreak, line3, linebreak)
    

    应该是

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

    但最好写成:

    target.write(' '.join(line1, linebreak, line2, linebreak, line3, linebreak))
    

    【讨论】:

    • target.write(linebreak.join([line1, line2, line3]))
    【解决方案3】:

    几天前我做了这个,在练习 22 中,他会要求你写下到目前为止所学的所有内容并记住它。

    早些时候他要求你评论每一行以解释它的作用。这可能是一个非常好的习惯,直到您无需考虑它们就知道线条的作用。

    还要注意你的陈述方式。

    如果你想成为一名程序员,你需要开始像一个人一样说话并使用词汇。

    line1、line2、line3、linebreak 称为变量。您使用 =(赋值运算符)临时给它们/赋值。

    Write 是一个函数。 python 中的函数在 () 之间接受参数。 所有参数都必须放在 () 内。例如,如果你这样写,我敢肯定 不会绊倒你的。

    stuffIWantToPrint = line1 + lb + line2 + lb+ line3 + lb #all strings together 
    target.write(stuffIWantToPrint) #pass the big string to write
    

    【讨论】:

      【解决方案4】:

      我来晚了,但你也这样做: target.write("{line1}\n{line2}\n{line3}\n").format(line1=line1,line2=line2,line3=line3)

      【讨论】:

        猜你喜欢
        • 2011-10-13
        • 1970-01-01
        • 2015-05-21
        • 2013-10-31
        • 2017-04-09
        • 1970-01-01
        • 1970-01-01
        • 2012-11-17
        相关资源
        最近更新 更多