我的代码有什么问题?python
我正在用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/
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缓存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”行。