Python 剥离和拆分如何剥离列表
我的代码:Python 剥离和拆分如何剥离列表,python,python-3.x,split,strip,Python,Python 3.x,Split,Strip,我的代码: readfile = open("{}".format(file), "r") lines = readfile.read().lower().split() elements = """,.:;|!@#$%^&*"\()`_+=[]{}<>?/~""" for char in elements: lines = lines.replace(char, '') readfile=open(“{}”。格式(文件),“r”) lines=readfile
readfile = open("{}".format(file), "r")
lines = readfile.read().lower().split()
elements = """,.:;|!@#$%^&*"\()`_+=[]{}<>?/~"""
for char in elements:
lines = lines.replace(char, '')
readfile=open(“{}”。格式(文件),“r”)
lines=readfile.read().lower().split()
elements=“”,.:;|!@$%^&*“\()”`` uU+=[]{}?/~”
对于元素中的字符:
行=行。替换(字符“”)
这会起作用并删除特殊字符。但我需要有关条带“-”和“”的帮助
例如,“安全舞”可以,但不是“-hi-”,但“我会”可以,但不是“hi”
我只需要去掉开头和结尾
它不是一个字符串,而是一个列表
我该怎么做呢?也许你可以试试
string.标点符号和strip
:
import string
my_string_list = ["-hello-", "safety-dance", "'hi", "I'll", "-hello"]
result = [item.strip(string.punctuation) for item in my_string_list]
print(result)
结果:
['hello', 'safety-dance', 'hi', "I'll", 'hello']
首先,在循环中使用str.replace
效率低下。因为字符串是不可变的,所以每次迭代都会创建一个需要的字符串。您可以使用str.translate
在一次循环中删除不需要的字符
至于仅当破折号不是边界字符时才删除破折号,这正是str.strip
所做的
您要删除的字符似乎也与字符串相对应。标点符号
,带有'-'
的特殊情况
from string import punctuation
def remove_special_character(s):
transltation = str.maketrans('', '', punctuation.replace('-', ''))
return ' '.join([w.strip('-') for w in s.split()]).translate(transltation)
polluted_string = '-This $string contain%s ill-desired characters!'
clean_string = remove_special_character(polluted_string)
print(clean_string)
# prints: 'This string contains ill-desired characters'
如果您想将其应用于多行,可以使用列表来实现
lines = [remove_special_character(line) for line in lines]
最后,要读取文件,应该使用with
语句
with open(file, "r") as f
lines = [remove_special_character(line) for line in f]
可能重复:可能重复的不是字符串,而是列表。@guide我还添加了关于如何使用with语句安全打开文件的建议,建议您阅读。