从字典中删除特殊字符 [英] Removing special characters from dictionary
问题描述
我正在尝试从python字典中删除所有\ r \ n.最简单的方法是这样做.我的字典目前看起来像这样-
{'':'34 .8 \ r \ n','Mozzarella di Giovanni \ r \ n':'34 .8 \ r \ n','Queso Cabrales \ r \ n':'14 \ r \ n','新加坡福建炒面\ r \ n':'9.8 \ r \ n'}
编辑:这是我正在尝试的-
对于键,productDictionary.items()中的值:key.strip()values.strip()key.strip('"\" r')key.strip('\\ n')values.strip('\\ r \\ n')印刷产品
输出还是一样.
您可以使用 str.strip()
:
str.strip()
不带任何参数时,将剥离所有类型的前导和尾随空格.
>>>productDictionary = {'':'34 .8 \ r \ n','Mozzarella di Giovanni \ r \ n':'34 .8 \ r \ n','Queso Cabrales \ r \ n':'14 \ r \ n','新加坡福建炒面\ r \ n':'9.8 \ r \ n'}>>>productDictionary = dict(productDictionary.items()中x的map(str.strip,x)>>>印刷产品>>>{'':'34 .8','Mozzarella di Giovanni':'34 .8','Queso Cabrales':'14','新加坡福建炒面':'9.8'}
str.strip()
help()
S.strip([chars])->字符串或Unicode
返回带有前导和尾随空格的字符串S的副本删除.如果给定chars而不是None,则删除chars中的字符反而.如果chars是unicode,则S将在转换为unicode之前剥离
I am trying to remove all the \r\n from the python dictionary. What is the easiest way to do so. My dictionary is looking like this at the moment -
{'': '34.8\r\n',
'Mozzarella di Giovanni\r\n': '34.8\r\n',
'Queso Cabrales\r\n': '14\r\n',
'Singaporean Hokkien Fried Mee\r\n': '9.8\r\n'
}
EDIT : Here's what I'm trying -
for key, values in productDictionary.items() :
key.strip()
values.strip()
key.strip('"\"r')
key.strip('\\n')
values.strip('\\r\\n')
print productDictionary
And the output is still the same.
you can use str.strip()
:
str.strip()
when used with no arguments, strips all types of leading and trailing whitespaces.
>>> productDictionary={'': '34.8\r\n',
'Mozzarella di Giovanni\r\n': '34.8\r\n',
'Queso Cabrales\r\n': '14\r\n',
'Singaporean Hokkien Fried Mee\r\n': '9.8\r\n'
}
>>> productDictionary=dict(map(str.strip,x) for x in productDictionary.items())
>>> print productDictionary
>>>
{'': '34.8',
'Mozzarella di Giovanni': '34.8',
'Queso Cabrales': '14',
'Singaporean Hokkien Fried Mee': '9.8'}
help()
on str.strip()
S.strip([chars]) -> string or unicode
Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping
这篇关于从字典中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!