字典中的Python打印项仅返回换行符

字典中的Python打印项仅返回换行符,python,list,dictionary,Python,List,Dictionary,项目:'1;保罗;克罗;28;男性;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

项目:
'1;保罗;克罗;28;男性;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]
行中,您正在检索文件第i行的最后一个字符。最后一个字符是
'\n'
。我想你想做的是:

friend = f[i].split(';')[-1] # This will return 2\n

它返回
'\n'
,因为在
friend=f[i][1]
行中,您正在检索文件中第i行的最后一个字符。最后一个字符是
'\n'
。我想你想做的是:

friend = f[i].split(';')[-1] # This will return 2\n

哇,这太明显了,我都没想到。非常感谢,先生!哇,这太明显了,我都没想到。非常感谢,先生!