我有这个字典:
dict1={'Period': '100.02\xa0minutes[2]', 'Repeat interval': '23\xa0days', 'Epoch': '25 January 2015, 00:45:13\xa0UTC[2]', 'Band': 'S Band(TT&C support)X Band(science data acquisition)', 'Bandwidth': 'up\xa0to\xa0722kbit/s\xa0download\xa0(S Band)up\xa0to\xa018.4Mbit/s\xa0download\xa0(X Band)up\xa0to\xa04kbit\xa0/s\xa0upload\xa0(S Band)'}
我要替换所有\xa0 by " "
我试试这个:
clean_dict = {key.strip(): item.strip() for key, item in dict1.items()}
但是输出是相同的。 我也尝试这样做:
new_keys = list(dict1.keys())
new_values = list(dict1.values())
new_keys2 = list()
new_values2 = list()
for element in new_keys:
print (element)
new_keys2.append(element.replace("\xa0", " "))
for element in new_values:
print(element)
new_values2.append(element.replace("\xa0", " "))
new_dict = dict(zip(new_keys2,new_values2))
但这也给了我相同的输出。如何解决该问题?
答案 0 :(得分:1)
您可以尝试replace
:
python 3.x
>>> dict2 = {k.replace(u'\xa0', ' ') : v.replace(u'\xa0', ' ') for k, v in dict1.items()}
或
python 2.7
>>> dict2 = {k.replace(u'\xa0', ' ') : v.replace(u'\xa0', ' ') for k, v in dict1.iteritems()}
结果是
>>> print(dict2)
{'Band': 'S Band(TT&C support)X Band(science data acquisition)',
'Bandwidth': 'up to 722kbit/s download (S Band)up to 18.4Mbit/s download (X Band)up to 4kbit /s upload (S Band)',
'Epoch': '25 January 2015, 00:45:13 UTC[2]',
'Period': '100.02 minutes[2]',
'Repeat interval': '23 days'}
答案 1 :(得分:1)
试试这个
{unicodedata.normalize("NFKD", key): unicodedata.normalize("NFKD", item) for key, item in dict1.items()}
输出
'Repeat interval': '23 days',
'Epoch': '25 January 2015, 00:45:13 UTC[2]',
'Band': 'S Band(TT&C support)X Band(science data acquisition)',
'Bandwidth': 'up to 722kbit/s download (S Band)up to 18.4Mbit/s download (X Band)up to 4kbit /s upload (S Band)'}