如何删除标点符号并使python的多个字符串语句的所有评论都变为小写

时间:2019-07-17 14:03:47

标签: python string punctuation

我喜欢3个字符串,如何删除标点符号并使所有评论变为小写,然后打印出所有3条评论。

Review1 = 'My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block.  It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm back in town.'

Review2 = 'Solid coffee in the Outram Park neighborhood. Location is hidden in a HDB block so you definitely need to search for it. Minus one star for limited seating options'

Review3 = 'Deserve it, truly deserves this much reviews. I will describe coffee here as honest, sincere, decent, strong, smart.'

5 个答案:

答案 0 :(得分:0)

Review1 = "My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block.  It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm back in town."

import string
Review1_Fixed = Review1.lower().translate(str.maketrans('', '', string.punctuation))
print(Review1_Fixed)

输出:

"my great auntie has lived at everton park for decades and once upon a time i even lived here too and i remember the days before when there was nothing remotely hipster about this housing block  it is really cool to see cute new cafes and coffee shops moving in and ive been to nylon every time im back in town"

有关此命令正在执行的操作的更多信息,或执行此操作的更多方法,请参见此post

答案 1 :(得分:0)

使用<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet exclude-result-prefixes="#all" version="3.0" xmlns="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.w3.org/2005/xpath-functions"> <xsl:param as="xs:string" name="json">[ { "logic":"ADD", "product":{ "productLength":5, "productUnits":"Kg" }, "numberOfUnits":1 }, { "logic":"ADD", "product":{ "productLength":5, "productUnits":"Kg" }, "numberOfUnits":2 } ] </xsl:param> <xsl:output method="text"/> <xsl:template match="/" name="xsl:initial-template"> <xsl:variable name="input-as-xml" select="json-to-xml($json)"/> <xsl:variable as="element(array)" name="transformed-xml"> <array> <xsl:for-each select="$input-as-xml/*/*"> <map xmlns="http://www.w3.org/2005/xpath-functions"> <string key="logic"> <xsl:value-of select="string[@key='logic']"/> </string> <map key="product"> <string key="productLength"> <xsl:value-of select="string[@key='stayLength/stayLength']"/> </string> <string key="productUnits"> <xsl:value-of select="string[@key='stayLength/stayLengthUnits']"/> </string> </map> <string key="numberOfUnits"> <xsl:value-of select="string[@key='numberOfUnits']"/> </string> </map> </xsl:for-each> </array> </xsl:variable> <xsl:value-of select="xml-to-json($transformed-xml, map { 'indent' : true() })"/> </xsl:template> </xsl:stylesheet> 模块:

re

打印:

Review1 = '''My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block. It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm back in town.'''
Review2 = '''Solid coffee in the Outram Park neighborhood. Location is hidden in a HDB block so you definitely need to search for it. Minus one star for limited seating options'''
Review3 = '''Deserve it, truly deserves this much reviews. I will describe coffee here as honest, sincere, decent, strong, smart.'''

import re

def strip_punctuation_make_lowercase(*strings):
    return map(lambda s: re.sub(r'[^\s\w]+', '', s).lower(), strings)

Review1, Review2, Review3 = strip_punctuation_make_lowercase(Review1, Review2, Review3)

print(Review1)
print()

print(Review2)
print()

print(Review3)
print()

答案 2 :(得分:0)

my great auntie has lived at everton park for decades and once upon a time i even lived here too and i remember the days before when there was nothing remotely hipster about this housing block it is really cool to see cute new cafes and coffee shops moving in and ive been to nylon every time im back in town

solid coffee in the outram park neighborhood location is hidden in a hdb block so you definitely need to search for it minus one star for limited seating options

deserve it truly deserves this much reviews i will describe coffee here as honest sincere decent strong smart

答案 3 :(得分:0)

__contains__方法定义了类实例出现在in运算符右侧而不是in运算符右侧的行为。

from string import ascii_letters

Review1 = "My great auntie has lived at Everton Park for decades, and once upon a time I even lived here too, and I remember the days before when there was nothing remotely hipster about this housing block.  It is really cool to see cute new cafes and coffee shops moving in, and I've been to Nylon every time I'm back in town."

key = set(ascii_letters + ' ') # key = set('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ')
Review1_ = ''.join(filter(key.__contains__, Review1)).lower()
print (Review1_)

输出:

my great auntie has lived at everton park for decades and once upon a time i even lived here too and i remember the days before when there was nothing remotely hipster about this housing block  it is really cool to see cute new cafes and coffee shops moving in and ive been to nylon every time im back in town

答案 4 :(得分:0)

用于删除标点符号 s.translate(无,字符串。标点符号) 或创建自己的功能 def标点(字符串):
    标点符号='''!()-[] {} ;:'“ \,<> ./?@#$%^&*_〜'''

for x in string.lower(): 
    if x in punctuations: 
        string = string.replace(x, "") 

# Print string without punctuation 
print(string) 

小写 string.lower()