开发者

Strange string concatenation on Python 2.5 in Windows

This code -

for ss_key in sd.keys():
    print str(sd[ss_key]) + ' ' + ss_key

prints the output as expected i.e. converts the int value of ss_key and prints the value and key on the same line.

But this code -

for ii_key in id.keys():
    print ii_key + ' ' + str(id[ii_key]),

prints key1 and then goes to a newline to print the value of key1开发者_运维百科 then key2 and goes to another new line to print value of key2.

Any pointers on this weird behaviour and how I can get the string concatenation right with key first and then value, Thanks a lot in advance.


I suppose that ii_key is a string, and the newline is part of that string. Try printing ii_key.rstrip().


You can check if your data contains linefeeds or other unwanted whitespaces by adding

print repr(ii_key)

This will print linefeeds and other special characters in escaped form (e.g. "\n").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜