【发布时间】:2015-07-14 20:13:40
【问题描述】:
我有一本这样的字典……
mydict = {"rows": [["col1", "col2", "col3"], ["testing data 1", "testing data 2lk\nIdrjy9dyj", "testing data 3"], ["testing data 2", "testing data 3", "testing data 4"], ["testing data 3", "testing data 4", "testing data 5"]],
"columns": ["col1", "col2", "col3"]}
我正在尝试用 html <br> 替换回车符 \n。
这是我得到的错误:
错误:“元组”对象没有“替换”属性
这是我正在尝试使用的代码...如果有人可以提供帮助,我将不胜感激。
for items in mydict['rows']:
i += 1
newitems = items.replace("\n", "<br>")
mydict['rows'][i] = newitems
【问题讨论】:
-
你的 mydict 包含列表,你的错误提到了元组。其中之一(或代码)一定是错误的。
-
list也没有replace方法。 -
当然可以,但是这里有更多的奇怪之处(比如
i是什么?这不会再出现一个错误吗?),让我们先把问题正确...
标签: python dictionary tuples str-replace