剥夺逗号和句号

时间:2013-03-20 22:50:53

标签: python file-io stripping

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

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

我的代码如下:

#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)

我目前得到的输出是:

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

正如您所看到的,“文件”和“行”这两个词的末尾有句号。

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

  

这是该文件的第一行。

     

这是第二行。

提前致谢。

3 个答案:

答案 0 :(得分:6)

这些行无效

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

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

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

答案 1 :(得分:4)

可能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

答案 2 :(得分:0)

试试这个:

 chars = [',', '.']

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