Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中从字符串的开头和结尾去除特殊字符_Python - Fatal编程技术网

如何在python中从字符串的开头和结尾去除特殊字符

如何在python中从字符串的开头和结尾去除特殊字符,python,Python,我有一个由特殊字符(!?@#$)组成的句子列表,位于字符串的末尾。我需要脱掉它们。以下是句子列表: ['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change your view of the matrix.', 'Are the human people the one

我有一个由特殊字符(!?@#$)组成的句子列表,位于字符串的末尾。我需要脱掉它们。以下是句子列表:

['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change your view of the matrix.', 'Are the human people the ones who started the war?', 'Is AI a bad thing?']
我的输出应该是这样的:

['The first time you see The Second Renaissance it may look boring', 'Look at it at least twice and definitely watch part 2', 'It will change your view of the matrix', 'Are the human people the ones who started the war', 'Is AI a bad thing']

如果只想从开头和结尾删除字符,可以使用
string.strip()
方法

例如:

strp_chars = '!?@#$.'
sentence = 'The first time you see The Second Renaissance it may look boring.'
print(sentence.strip(strp_chars))

如果只想从开头和结尾删除字符,可以使用
string.strip()
方法

例如:

strp_chars = '!?@#$.'
sentence = 'The first time you see The Second Renaissance it may look boring.'
print(sentence.strip(strp_chars))

只需在列表压缩中使用
string.strip
,删除所有需要删除的字符,例如:

In [1]: l = ['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change
   ...:  your view of the matrix.', 'Are the human people the ones who started the war?', 'Is AI a bad thing?']

In [2]: p = [i.strip('.,?!') for i in l]

In [3]: p
Out[3]:
['The first time you see The Second Renaissance it may look boring',
 'Look at it at least twice and definitely watch part 2',
 'It will change your view of the matrix',
 'Are the human people the ones who started the war',
 'Is AI a bad thing']

In [4]:

只需在列表压缩中使用
string.strip
,删除所有需要删除的字符,例如:

In [1]: l = ['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change
   ...:  your view of the matrix.', 'Are the human people the ones who started the war?', 'Is AI a bad thing?']

In [2]: p = [i.strip('.,?!') for i in l]

In [3]: p
Out[3]:
['The first time you see The Second Renaissance it may look boring',
 'Look at it at least twice and definitely watch part 2',
 'It will change your view of the matrix',
 'Are the human people the ones who started the war',
 'Is AI a bad thing']

In [4]:

您可以尝试转换方法:

import unicodedata
import sys

data1=['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change your view of the matrix.', 'Are the human people the ones who started the war?', 'Is AI a bad thing?']


data=dict.fromkeys([i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P')])

def remove_punctuation(sentence):
    return sentence.translate(data)

for i in data1:
    print(remove_punctuation(i))
输出:

The first time you see The Second Renaissance it may look boring
Look at it at least twice and definitely watch part 2
It will change your view of the matrix
Are the human people the ones who started the war
Is AI a bad thing

您可以尝试转换方法:

import unicodedata
import sys

data1=['The first time you see The Second Renaissance it may look boring.', 'Look at it at least twice and definitely watch part 2.', 'It will change your view of the matrix.', 'Are the human people the ones who started the war?', 'Is AI a bad thing?']


data=dict.fromkeys([i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P')])

def remove_punctuation(sentence):
    return sentence.translate(data)

for i in data1:
    print(remove_punctuation(i))
输出:

The first time you see The Second Renaissance it may look boring
Look at it at least twice and definitely watch part 2
It will change your view of the matrix
Are the human people the ones who started the war
Is AI a bad thing

“特殊字符”的定义是什么?如果每个字符串都有一个特殊字符super@chrisz“特殊字符”的定义是什么?如果每个字符串都有一个特殊字符super@克丽丝扎维索姆。工作得很好。非常感谢,太棒了。工作得很好。非常感谢你。