Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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_Python 2.7 - Fatal编程技术网

我的代码有什么问题?python

我的代码有什么问题?python,python,python-2.7,Python,Python 2.7,我正在用python编程,但是我遇到了一个我无法解决的小故障!问题是,当它在文本文件中打印时,它只打印整个输出的一行!否则就行了!求你了,我需要帮助才能成功 import sys, bz2, string, os #instead of hardcoding filename, get it from arguments #filename = os.getcwd() filename = raw_input("Enter the path of bz2 document e.g. files/

我正在用python编程,但是我遇到了一个我无法解决的小故障!问题是,当它在文本文件中打印时,它只打印整个输出的一行!否则就行了!求你了,我需要帮助才能成功

import sys, bz2, string, os
#instead of hardcoding filename, get it from arguments
#filename = os.getcwd()
filename = raw_input("Enter the path of bz2 document e.g. files/access_log-20130301.bz2: ")
print "Using file : " + filename
source_file = bz2.BZ2File(filename, "r") 
for line in source_file:
    #Extract the date and put into a variable 
    logdate = string.split(line)[3][1:12]
    #Extract movie name and put into variable movie
    movie = string.split(line)[6]
    #extract who read the movie username = 
    usernames = string.split(line)[2]
    #Only process the movie line if we have /media/movie in it. 
    if movie.find('media/movies') > 0:
        #Prints all things prosscesed
        print "User:" + usernames + " On:" +  logdate + " Was watching:"+ movie
        #p=open(filename+"record.txt", "w")
        fp=open(filename+"record.txt", "wb+")
        fp.write("User: " + usernames + " On: " +  logdate + " Was watching: "+ movie+" File from:"+filename+"\n")
sys.exit()

问题可能是,每次要写入一行时,您都要为该文件打开一个新的文件句柄,并且没有首先刷新输出。这里有两种可能的解决方案:

  • 在主
    for
    循环之前打开要写入的文件。这样,您将只有一个文件句柄,缺少刷新不会导致此行为。确保完成后关闭该文件。(考虑将
    块一起使用,这将导致文件在块终止时自动关闭:
    与open(filename+“record.txt”,“wb+”)一起作为f:
  • 在调用
    fp.write()
    后立即关闭
    fp
    ,这将强制刷新所有缓冲输出,至少刷新到内核I/O缓存
  • 我更喜欢选项1,因为在这种情况下没有理由多次打开和关闭文件。(如果要在文件中写入许多行,这些打开/刷新/关闭循环将浪费大量时间!)

    选项1看起来像这样:

    import sys, bz2, string, os
    #instead of hardcoding filename, get it from arguments
    #filename = os.getcwd()
    filename = raw_input("Enter the path of bz2 document e.g. files/access_log-20130301.bz2: ")
    print "Using file : " + filename
    with open(filename+"record.txt", "wb+") as fp:
        source_file = bz2.BZ2File(filename, "r") 
        for line in source_file:
            #Extract the date and put into a variable 
            logdate = string.split(line)[3][1:12]
            #Extract movie name and put into variable movie
            movie = string.split(line)[6]
            #extract who read the movie username = 
            usernames = string.split(line)[2]
            #Only process the movie line if we have /media/movie in it. 
            if movie.find('media/movies') > 0:
                #Prints all things prosscesed
                print "User:" + usernames + " On:" +  logdate + " Was watching:"+ movie
                #p=open(filename+"record.txt", "w")
                fp.write("User: " + usernames + " On: " +  logdate + " Was watching: "+ movie+" File from:"+filename+"\n")
    
    # The with block has ended at this point, so the file will already be closed here.
    
    sys.exit()
    

    您正在循环内以写模式打开输出文件。在主循环外打开它一次

    完成后一定要把它关上。更妙的是,这样写:

    with open(filename + "record.txt", "wb+") as fp:
        for line in source_file:
            ...
            fp.write(...)
    

    因此,
    open
    上下文管理器会在之后为您关闭它。

    作为一个说明,在处理文件时,最好使用它来打开和关闭它们。在我看来,这更像是
    Python 2
    。为什么两个标签都有?你能上传你的输出吗?可能是Dropbox、SkyDrive或Pastebin链接?问题是,
    open(filename+“record.txt”,“w”)
    每次都会截断文件,因此您只能得到最后一行。使用“a”模式进行追加,或者更好的是,按照@Kirk Strauser的回答将开放从循环中拉出。模式不应该是“w”而不是“wb+”吗?“b”表示二进制,这将在windows上导致时髦的行尾。“+”表示可读,但它只被写入。是的,可能。我试图显示最小数量的更改,以便他能够看到“before”行如何映射到“after”行。