简体   繁体   English

从没有Unicode的键中打印字典值[u'STRING]

[英]Print Dictionary Value From Key Without The Unicode [u'STRING]

I am adding to a dictionary by doing .append within python 我通过在python中执行.append添加到字典

theDict = defaultdict(list)
theDict[theKey].append(theValue)

I'll try to print it out via 我会尝试通过打印出来

print theDict[valueKeyToLookUp]

But it always ends up looking like this: 但是它总是最终看起来像这样:

[u'STRING_I_WANT']

I've tried doing print with str() at the front, but it still leaves the u' there. 我尝试过在前面使用str()进行打印,但是仍然会留下u。

I tried this: Python string prints as [u'String'] by doing 我试过了:通过执行以下操作, Python字符串打印为[u'String']

valueKeyToLookUp.encode('ascii')

But it still comes out the same. 但结果还是一样。 Other solutions look like they are looking up values via the position it is in the list by number, but I need to do it by value. 其他解决方案看起来就像他们通过按数字在列表中的位置查找值一样,但是我需要按值来执行。

Any ideas? 有任何想法吗?

Your dictionary doesn't contain strings, it contains lists of strings. 您的字典不包含字符串,而是包含字符串列表 When you print a list, that's the way Python formats it. 当您打印列表时,这就是Python格式化列表的方式。

If you know that the list only contains a single string, just print that: 如果您知道列表仅包含一个字符串,则只需打印以下内容:

print theDict[valueKeyToLookUp][0]

If the dictionary isn't supposed to contain lists, you need to fix that. 如果字典不应该包含列表,则需要对其进行修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM