Reputation: 488
I have this dict:
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)'}
I want to replace all \xa0 by " "
I try this:
clean_dict = {key.strip(): item.strip() for key, item in dict1.items()}
But the output is the same. I try this, too:
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))
But this also gives me the same output. How do I fix the problem?
Upvotes: 2
Views: 3301
Reputation: 21
Try This
{unicodedata.normalize("NFKD", key): unicodedata.normalize("NFKD", item) for key, item in dict1.items()}
Output
'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)'}
Upvotes: 2
Reputation: 1637
You can try replace
:
python 3.x
>>> dict2 = {k.replace(u'\xa0', ' ') : v.replace(u'\xa0', ' ') for k, v in dict1.items()}
or
python 2.7
>>> dict2 = {k.replace(u'\xa0', ' ') : v.replace(u'\xa0', ' ') for k, v in dict1.iteritems()}
The result is
>>> 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'}
Upvotes: 1