Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:遍历文件中的行_Python_Loops - Fatal编程技术网

Python:遍历文件中的行

Python:遍历文件中的行,python,loops,Python,Loops,我试图使用python迭代两个文件,每个文件中有多行。我想做的是,将两个文件中的每一行分解成单词,然后将“query”文件中当前行中的每个单词与“tweet”文件中当前行中的每个单词进行比较。以上是我到目前为止所做的,但它只适用于查询文件中的第一行,并跳过了其中的其余行。它确实适用于tweet文件中的每一行。有什么帮助吗 编辑重复注释:我知道在迭代查询文件之后,文件句柄将定位在EOF。但是我不明白为什么它不处理查询文件中的下一行,而直接转到EOF。本质上,发生的是,您在查看另一个文件中的第一行的

我试图使用python迭代两个文件,每个文件中有多行。我想做的是,将两个文件中的每一行分解成单词,然后将“query”文件中当前行中的每个单词与“tweet”文件中当前行中的每个单词进行比较。以上是我到目前为止所做的,但它只适用于查询文件中的第一行,并跳过了其中的其余行。它确实适用于tweet文件中的每一行。有什么帮助吗


编辑重复注释:我知道在迭代查询文件之后,文件句柄将定位在EOF。但是我不明白为什么它不处理查询文件中的下一行,而直接转到EOF。

本质上,发生的是,您在查看另一个文件中的第一行的同时,遍历了一个文件中的所有行。您不能在下一次迭代中遍历这些行,因为您已经读出了它们

这样做:

queries = open(sys.argv[1],"rU")
tweets = open(sys.argv[2],"rU")
for query in queries:
    for tweet in tweets:
        query_words = query.split()
        tweet_words = tweet.split()
        for qword in query_words:
            for tword in tweet_words:
               #Comparison

问题是,在遍历文件的每一行之后,您处于
EOF
。您必须再次打开它,或者在读取或迭代到下一行之前,确保每一行都按预期进行了处理(在您的示例中是拆分和比较的)。在您的示例中,由于文件
tweets
query
的第一次迭代后处于
EOF
,因此文件
querys
“跳过”到
EOF
开始第二次迭代,这仅仅是因为嵌套循环中没有更多的
tweet
可以迭代

此外,尽管垃圾收集为您处理文件关闭,但显式关闭每个打开的文件仍然是更好的做法


请参阅以进行修改。

使用函数文件.readline()而不是执行类似的循环

mirosval提供了一个更简单的答案,使用他的

考虑使用:


您希望为第一个文件的每一行迭代第二个文件。但看看会发生什么:

  • 你打开两个文件
  • 开始迭代第一个文件
  • 获取第一个文件的第一行
  • 迭代第二个文件,直到第二个文件的end=>指针位于EOF
  • 您尝试处理第一个文件的第二行
  • 第二个文件的指针已经在EOF,您可以立即循环第一个文件的下一行,而无需任何处理
所以,在第一个文件的每次迭代之后,您必须倒带第二个文件。有两种方法可以做到这一点:

  • 将第二个文件加载到内存中,作为具有
    readlines
    的行列表,并遍历该列表。因为它是一个列表(而不是一个文件),所以迭代将从第一个位置开始,而不是从当前位置开始

    with open(sys.argv[1],"rU") as queries:
        with open(sys.argv[2],"rU") as tweets:
            for query in queries:
                query_words = query.split()
                for tweet in tweets:
                    tweet_words = tweet.split()
                    for qword in query_words:
                        for tword in tweet_words:
                            #Comparison
                tweets.seek(0) # go back to the start of the file
    
  • 使用
    skip

    queries = open(sys.argv[1],"rU")
    tweets_file = open(sys.argv[2],"rU")
    tweets = tweets_file.readlines() # tweets is now a list of lines
    for query in queries:
        for tweet in tweets:
            query_words = query.split()
            tweet_words = tweet.split()
            for qword in query_words:
                for tword in tweet_words:
                   #Comparison
    

第一个解决方案只读取第二个文件一次,但占用更多内存。如果第二个文件较小(在最近的机器上小于几百个Mo),则应优先选择该文件。第二个解决方案使用更少的内存,应该优先考虑的是第二个文件是巨大的。。。或者,如果出于任何原因(嵌入式系统,降低脚本的影响…)需要节省内存,则可能需要使用querys.readlines()和tweets.readlines()。其工作原理如下:1查询-1推特2查询-2推特。我想做的是:1查询-1推1查询-2推2查询-1推2查询-2推。基本上,在进入下一个查询之前,我想将一个查询与每个tweet进行比较。然后,你可以进行双for循环,现在也可以了,因为我们使用
readlines()
获得了查询和tweet,所以你可以在这两个查询中重复多次,而不必运行EOFThis works,Thomas解释了为什么会这样做。谢谢你们两个@SajalSharma是的,我认为这是对你来说最好的答案。没问题。:)或者,您可以将数据存储在一个列表中,而不是将tweets文件读取x次(取决于查询文件中的行数),这对您的硬盘驱动器更有利,除非您不能使用太多的RAM。
with open(sys.argv[1],"rU") as queries:
    with open(sys.argv[2],"rU") as tweets:
        for query in queries:
            query_words = query.split()
            for tweet in tweets:
                tweet_words = tweet.split()
                for qword in query_words:
                    for tword in tweet_words:
                        #Comparison
            tweets.seek(0) # go back to the start of the file
queries = open(sys.argv[1],"rU")
tweets_file = open(sys.argv[2],"rU")
tweets = tweets_file.readlines() # tweets is now a list of lines
for query in queries:
    for tweet in tweets:
        query_words = query.split()
        tweet_words = tweet.split()
        for qword in query_words:
            for tword in tweet_words:
               #Comparison
queries = open(sys.argv[1],"rU")
tweets = open(sys.argv[2],"rU")
for query in queries:
    for tweet in tweets:
        query_words = query.split()
        tweet_words = tweet.split()
        for qword in query_words:
            for tword in tweet_words:
               #Comparison
    tweets.seek(0) # explicitely rewind tweets