[英]Python print item from dictionary returns newline symbol only
項目: '1;Paul;Crowe;28;male;2\\n'
碼:
f = open('data.txt', 'r+').readlines()
l = len(f)
data = {}
friends = {}
for i in range(0,l):
person = f[i].split(';')
friend = f[i][-1]
data.update({person[0]:person[1:]})
friends.update({osoba[0]:friend})
print friends
並輸出打印結果:
{'11': '\n', '10': '\n', '13': '\n', '12': '\n', '15': '\n', '14': '\n', '17': '\n', '16': '\n', '19': '\n', '18': '\n', '20': '\n', '1': '\n', '3': '\n', '2': '\n', '5': '\n', '4': '\n', '7': '\n', '6': '\n', '9': '\n', '8': '\n'}
我不明白為什么它不返回'2 \\ n'字符串。 似乎是什么問題?
它返回'\\n'
因為在friend = f[i][-1]
行中,您正在檢索文件中第ith行的最后一個字符。 最后一個字符是'\\n'
。 我想您想做的是:
friend = f[i].split(';')[-1] # This will return 2\n
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.