【问题标题】:having problems with .replace() [duplicate].replace() 有问题 [重复]
【发布时间】:2016-09-12 18:46:03
【问题描述】:

我在替换“?”时遇到问题零。 ogtL 只是我从 excel 文件中获取的数字列表。从那个 excel 中获得的只是更多的数字。我只是对它为什么不工作有疑问,我还希望在将新字符串放回 ogtL 方面得到一些帮助。

for eachWord in ogtL: #ogtL is a List
    newString=""
    for eachCharacter in eachWord:
        newString+=eachCharacter
        newString.replace("?","0")
    print(newString)

【问题讨论】:

  • 你得到的输出是什么?

标签: python string list replace


【解决方案1】:

我认为问题在于您没有将替换的输出保存到变量中。

for eachWord in ogtL: #ogtL is a List
    newString=""
    for eachCharacter in eachWord:
        newString+=eachCharacter
        newString = newString.replace("?","0")
    print(newString)

你也不需要像那样迭代每个字符,我认为你可以这样做:

for eachWord in ogtL: #ogtL is a List
    newString=eachWord.replace("?","0")        
    print(newString)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-07
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多