【发布时间】:2014-04-06 06:19:34
【问题描述】:
所以我有一个类似的文本文件:
apples,green
tomatos,red
bananas,yellow
我将其用作字典的代码是
def load_list(filename):
with open(filename, "rU") as my_file:
my_list = {}
for line in my_file:
x = line.split(",")
key = x[0]
value = x[1]
my_list[key] = value
print my_list
工作正常,除了每个值都有 \n 添加到它的末尾,因为换行符。我尝试添加
.strip()
到x属性,但它导致属性错误(AttributeError:'dict'对象没有属性'strip')。
那么如何删除\n?
【问题讨论】:
标签: python python-2.7 dictionary