如何获取一个txt文件并将其拆分为字符串,消除任何浮动或

2024-10-04 03:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

编程新手,使用python3.0。你知道吗

我必须编写一个程序,为一个.txt文件输入一个文件名,读取该文件,但只读取某些字,忽略浮点和整数以及任何与另一个列表中的任何字都不匹配的字。你知道吗

基本上,我有单词列表和消息.txt. 这个程序必须通读消息.txt(文本示例:

[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with the love of my life ... ARREIC)

然后它必须忽略所有数字,搜索消息中的任何单词是否与words列表中的单词匹配,然后将这些单词与hvList中的值(int)匹配。你知道吗

到目前为止,我所拥有的:(wordsList和hvList在代码的另一部分,我认为没有必要显示它来理解我正在尝试做什么(如果您确实需要它来帮助,请告诉我)

def tweetsvalues ():
    tweetwList = []
    tinputfile = open(tweetsinputfile,"r")
    for line in tinputfile:
        entries = line.split()

最后一行entries = line.split()我猜是需要修改的。你知道吗


Tags: 文件to程序txt消息列表文件名编程
2条回答

这是我今天早些时候作为一个非常基本的拼写检查器编写的一些代码。我想更具体地回答你的问题,但我现在没有时间。这应该可以完成你想要的。我正在打开的硬编码的.txt文件包含许多拼写正确的英语单词。请随时补充我的想法到您的工作需要,但一定要了解所有的代码,你正在使用,否则我只会阻碍你的学习,把这个代码给你。在您的情况下,您可能希望输出所有单词,而不管它们的拼写如何,在我的代码中,我只输出拼写错误的单词。有问题尽管问

    #                            -
    # The "spellCheck" function determines whether the input
    # from the inputFile is a correctly spelled word, and if not
    # it will return the word and later be written to a file
    # containing misspelled words
    #                            -
    def spell_check(word, english):
        if word in english:
            return None
        else:
            return word

    #                            -
    # The main function will include all of the code that will
    # perform actions that are not contained within our other
    # functions, and will generally call on those other functions
    # to perform required tasks
    #                            -
    def main():
        # Grabbing user input
        inputFile = input('Enter the name of the file to input from: ')
        outputFile = input('Enter the name of the file to output to: ')
        english = {}  # Will contain all available correctly spelled words.
        wrong = []  # Will contain all incorrectly spelled words.
        num = 0  # Used for line counter.

        # Opening, Closing, and adding words to spell check dictionary
        with open('wordlist.txt', 'r') as c:
            for line in c:
                (key) = line.strip()
                english[key] = ''

        # Opening, Closing, Checking words, and adding wrong ones to wrong list
        with open(inputFile, 'r') as i:
            for line in i:
                line = line.strip()
                fun = spell_check(line, english)
                if fun is not None:
                    wrong.append(fun)

        # Opening, Closing, and Writing to output file
        with open(outputFile, 'w') as o:
            for i in wrong:
                o.write('%d %s\n' % (num, i))
                num += 1

    main()

是的,斯普利特是你最好的朋友。您还可以在文档中查找is方法。尽管完整的代码超出了StackOverflow的正常范围,但是您的中心工作看起来像

words = sentence.split()
good_words = [word for word in words if isalpha(word)]

这可以用一种“Pythonic”的方式来完成,使用过滤器,删除标点符号等。但是,从你在帖子中的写作方式来看,我怀疑你可以从这里开始。你知道吗

相关问题 更多 >