Python 去除逗号和句号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15536287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:21:10  来源:igfitidea点击:

Stripping Commas and Periods

pythonfile-iostripping

提问by Keyfer Mathewson

I am currently trying to input a text file, separate each word and organize them into a list.

我目前正在尝试输入一个文本文件,将每个单词分开并将它们组织成一个列表。

The current problem I'm having is getting rid of commas and periods from the text file.

我目前遇到的问题是从文本文件中删除逗号和句点。

My code is below:

我的代码如下:

#Process a '*.txt' file.
def Process():
    name = input("What is the name of the file you would like to read from? ")

    file = open( name , "r" )
    text = [word for line in file for word in line.lower().split()]
    word = word.replace(",", "")
    word = word.replace(".", "")

    print(text)

The output I'm currently getting is this:

我目前得到的输出是这样的:

['this', 'is', 'the', 'first', 'line', 'of', 'the', 'file.', 'this', 'is', 'the', 'second', 'line.']

As you can see, the words "file" and "line" have a period at the end of them.

如您所见,“file”和“line”这两个词的末尾有一个句点。

The text file I'm reading is:

我正在阅读的文本文件是:

This is the first line of the file.

This is the second line.

这是文件的第一行。

这是第二行。

Thanks in advance.

提前致谢。

回答by jamylak

These lines have no effect

这些线没有效果

word = word.replace(",", "")
word = word.replace(".", "")

just change your list comp to this:

只需将您的列表组合更改为:

[word.replace(",", "").replace(".", "") 
 for line in file for word in line.lower().split()]

回答by John La Rooy

Maybe stripis more appropriate than replace

也许stripreplace

def Process():
    name = input("What is the name of the file you would like to read from? ")

    file = open(name , "r")
    text = [word.strip(",.") for line in file for word in line.lower().split()]
    print(text)
>>> help(str.strip)
Help on method_descriptor:

strip(...)
    S.strip([chars]) -> string or unicode

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    If chars is unicode, S will be converted to unicode before stripping

回答by Tareq

Try this:

尝试这个:

 chars = [',', '.']

 word.translate(None, ''.join(chars))

For Python3

对于 Python3

 chars = [',', '.']
 word.translate({ord(k): None for k in chars})