删除列表中的空行和额外空间

2024-05-21 01:54:55 发布

您现在位置:Python中文网/ 问答频道 /正文

列表中的数据如下所示:

data = [u'Gluten free menu includes gluten free rolls for sandwiches and gluten free brownies! There are also many dishes that can be adjusted to be gluten free.',
 u"They use the same slicing machine for diary and meats.  Manager was knowledgable about food prep, servers were not.\r\r\r\n\r\r\r\nWe've been very select about which foods to order, but haven't had any issues.",
 u'The restaurant has a whole gluten free section of the menu and says that they will accommodate any allergies.  I myself do not have allergies but grandchildren have dairy, egg nut and niece has gluten.',
 u'Got sick from crosscontam Bfast. I suggest ordering the Mexican dishes if cross contam is an issue. My fav place in Sedona tho food is great!']

我想要的结果是:

Gluten free menu includes gluten free rolls for sandwiches and gluten free brownies! There are also many dishes that can be adjusted to be gluten free.
They use the same slicing machine for diary and meats.  Manager was knowledgable about food prep, servers were not. We've been very select about which foods to order, but haven't had any issues.
The restaurant has a whole gluten free section of the menu and says that they will accommodate any allergies.  I myself do not have allergies but grandchildren have dairy, egg nut and niece has gluten.
Got sick from crosscontam Bfast. I suggest ordering the Mexican dishes if cross contam is an issue. My fav place in Sedona tho food is great!

我尝试了不同的代码,包括以下示例:

for sentence in data:
    print sentence.strip() 

似乎什么都没用。有什么建议吗?提前谢谢


Tags: andthetofreeforthatfoodnot
2条回答

假设多行字符串是使用三个引号正确创建的

str.strip()只去除尾随和前导空格,而不是中间的空格

最简单的方法是首先split()任何空格上的完整字符串,然后str.join()使用一个空格返回

示例-

for sentence in data:
    print ' '.join(sentence.split())

用编辑过的真实数据演示-

>>> data = [u'Gluten free menu includes gluten free rolls for sandwiches and gluten free brownies! There are also many dishes that can be adjusted to be gluten free.',
...  u"They use the same slicing machine for diary and meats.  Manager was knowledgable about food prep, servers were not.\r\r\r\n\r\r\r\nWe've been very select about which foods to order, but haven't had any issues.",
...  u'The restaurant has a whole gluten free section of the menu and says that they will accommodate any allergies.  I myself do not have allergies but grandchildren have dairy, egg nut and niece has gluten.',
...  u'Got sick from crosscontam Bfast. I suggest ordering the Mexican dishes if cross contam is an issue. My fav place in Sedona tho food is great!']
>>>
>>> for sentence in data:
...     print(' '.join(sentence.split()))
...
Gluten free menu includes gluten free rolls for sandwiches and gluten free brownies! There are also many dishes that can be adjusted to be gluten free.
They use the same slicing machine for diary and meats. Manager was knowledgable about food prep, servers were not. We've been very select about which foods to order, but haven't had any issues.
The restaurant has a whole gluten free section of the menu and says that they will accommodate any allergies. I myself do not have allergies but grandchildren have dairy, egg nut and niece has gluten.
Got sick from crosscontam Bfast. I suggest ordering the Mexican dishes if cross contam is an issue. My fav place in Sedona tho food is great!

给出(你的例子):

>>> data = [u'Gluten free menu includes gluten free rolls for sandwiches and gluten free brownies! There are also many dishes that can be adjusted to be gluten free.',
...  u"They use the same slicing machine for diary and meats.  Manager was knowledgable about food prep, servers were not.\r\r\r\n\r\r\r\nWe've been very select about which foods to order, but haven't had any issues.",
...  u'The restaurant has a whole gluten free section of the menu and says that they will accommodate any allergies.  I myself do not have allergies but grandchildren have dairy, egg nut and niece has gluten.',
...  u'Got sick from crosscontam Bfast. I suggest ordering the Mexican dishes if cross contam is an issue. My fav place in Sedona tho food is great!']

你可以做:

>>> import re
>>> data=[re.sub(r'[\n\r]', '', e) for e in data]
>>> print '\n'.join(data)

印刷品:

Gluten free menu includes gluten free rolls for sandwiches and gluten free brownies! There are also many dishes that can be adjusted to be gluten free.
They use the same slicing machine for diary and meats.  Manager was knowledgable about food prep, servers were not.We've been very select about which foods to order, but haven't had any issues.
The restaurant has a whole gluten free section of the menu and says that they will accommodate any allergies.  I myself do not have allergies but grandchildren have dairy, egg nut and niece has gluten.
Got sick from crosscontam Bfast. I suggest ordering the Mexican dishes if cross contam is an issue. My fav place in Sedona tho food is great!

相关问题 更多 >