从字符串列表中删除特定字符-Python

从字符串列表中删除特定字符-Python,python,replace,Python,Replace,我有一个文件,我从中读取了一组单词,这个文件是“file1.txt” “file1.txt”文件的示例内容如下: Hello how are you? Very good! Hello how are you Very good def main(): characters = '!?¿-.:;' with open('file1.txt') as f: aux = ''.join(c for c in f.read() if c not in chara

我有一个文件,我从中读取了一组单词,这个文件是“file1.txt”

“file1.txt”文件的示例内容如下:

Hello how are you? Very good!
Hello how are you Very good
def main():

    characters = '!?¿-.:;'

    with open('file1.txt') as f:
        aux = ''.join(c for c in f.read() if c not in characters)

    # print(aux) # Hello how are you Very good
我要做的是消除示例中出现的符号字符

对于上一个示例,最后一个短语如下:

Hello how are you? Very good!
Hello how are you Very good
def main():

    characters = '!?¿-.:;'

    with open('file1.txt') as f:
        aux = ''.join(c for c in f.read() if c not in characters)

    # print(aux) # Hello how are you Very good
我的想法是,阅读完所有单词后,将它们存储在一个列表中,应用相应的“替换”来删除所有类型的无效字符

我认为的另一个想法是,当我加载.txt文件时,直接在那里应用replace,但是在尝试不同的方法之后,我不会应用对无效字符的删除

这是我的密码:

# -*- coding: utf-8 -*-

import sys 


def main():

  characters = '!?¿-.:;'
  aux = []

  with open('file1.txt','r') as f:
    for line in f:
      for word in line.split():
        aux.append(word)

  for a in aux:
    for character in characters:
      a = a.replace(character,"")

if __name__ == '__main__':
    main()
如您所见,我的代码的第一部分将txt文件中的所有单词存储在一个名为“aux”的列表中


但是我不知道如何应用“替换”方法来消除单词中的无效字符。

您只是重新分配循环变量,而不是改变列表!将最后一个循环更改为:

for i in range(len(aux)):
  for character in characters:
    # this actually changes the list element
    aux[i] = aux[i].replace(character, "")  
您的旧版本大致相当于:

for i in range(len(aux)):
  a = aux[i]
  for character in characters:
    a = a.replace(character, "") 
    # aux[i] is unimpressed ;)

您只是重新分配循环变量,而不是改变列表!将最后一个循环更改为:

for i in range(len(aux)):
  for character in characters:
    # this actually changes the list element
    aux[i] = aux[i].replace(character, "")  
您的旧版本大致相当于:

for i in range(len(aux)):
  a = aux[i]
  for character in characters:
    a = a.replace(character, "") 
    # aux[i] is unimpressed ;)

通过直接遍历文件并将其内容写入变量,过滤掉不需要的字符,可以实现更简单的方法

例如,下面是包含以下内容的
'file1.txt'
文件:

你好,你好吗?非常好! 然后,我们可以执行以下操作:

Hello how are you? Very good!
Hello how are you Very good
def main():

    characters = '!?¿-.:;'

    with open('file1.txt') as f:
        aux = ''.join(c for c in f.read() if c not in characters)

    # print(aux) # Hello how are you Very good
如我们所见,aux是文件的内容,没有不需要的字符,可以根据所需的输出格式轻松编辑

例如,如果我们想要一个单词列表,我们可以这样做:

def main():

    characters = '!?¿-.:;'

    with open('file1.txt') as f:
        aux = ''.join(c for c in f.read() if c not in characters)
        aux = aux.split()

    # print(aux) # ['Hello', 'how', 'are', 'you', 'Very', 'good']

通过直接遍历文件并将其内容写入变量,过滤掉不需要的字符,可以实现更简单的方法

例如,下面是包含以下内容的
'file1.txt'
文件:

你好,你好吗?非常好! 然后,我们可以执行以下操作:

Hello how are you? Very good!
Hello how are you Very good
def main():

    characters = '!?¿-.:;'

    with open('file1.txt') as f:
        aux = ''.join(c for c in f.read() if c not in characters)

    # print(aux) # Hello how are you Very good
如我们所见,aux是文件的内容,没有不需要的字符,可以根据所需的输出格式轻松编辑

例如,如果我们想要一个单词列表,我们可以这样做:

def main():

    characters = '!?¿-.:;'

    with open('file1.txt') as f:
        aux = ''.join(c for c in f.read() if c not in characters)
        aux = aux.split()

    # print(aux) # ['Hello', 'how', 'are', 'you', 'Very', 'good']

你必须直接处理aux[xxx],或者使用列表理解来重建它。你必须直接处理aux[xxx],或者使用列表理解来重建它。Omg完全正确,thanksOmg完全正确,Thanksys更简单,但我不知道为什么你的想法的解决方案会给我所有用元音分隔的单词,例如“hello”它给我的单词是“h”“e”“l”“l”“o”@fiticida,
aux
这里只是一个文件的内容,没有不需要的字符。它可以根据您想要的输出类型轻松编辑。@fiticida,例如,如果您想要单词列表,只需像这样添加
aux=aux.split()
?aux=aux.split().join(如果c不是字符,则c代表f.read()中的c)。很抱歉,我是python新手,列出了想念我的原因now@fiticida,检查最后一次编辑。你也可以在一行中实现
split()
,但是在这种情况下
split()
应该在末尾:
aux='''.join(如果c不是字符,那么f.read()中的c代表c)。split()
是更简单,但我不知道为什么你的想法的解决方案会给我所有用元音分隔的单词,例如它给我的“hello”单词“h”“e”“l”“l”“o”@fiticida,
aux
这里只是一个文件的内容,没有不需要的字符。它可以根据您想要的输出类型轻松编辑。@fiticida,例如,如果您想要单词列表,只需添加
aux=aux.split()
类似的内容?aux=aux.split().join(c代表f中的c,如果c不是字符,则读取())。很抱歉,我是python新手,列表中有我的想念now@fiticida,检查最后一次编辑。您也可以在一行中实现
split()
,但是在这种情况下
split()
应该在末尾:
aux=''.join(如果c不是字符,则c代表f中的c.read())。split()