Python脚本中的MemoryError

嗨,
我已经编写了一个小的Python脚本来解析非常大的数据集.它工作了一段时间,然后停止,并显示一条错误消息:
回溯(最近一次呼叫):
文件"./tmveritraceparser.py",第33行,in?
对于fin.xreadines()中的lineStr:
内存错误
代码片段为:

选择 | 换行 | 行号
  1. for lineStr in fin.xreadlines():
  2.     operAddress = ParseLine(lineStr)
  3.     address = operAddress[2].split('x')
  4.     if (operAddress[1] == 'write'): 
  5.         fout.write("%5s write %5s \n" % (operAddress[0], address[1]))
  6.     if (operAddress[1] == 'read'):
  7.       #  print operAddress[0] +  address[1]
  8.         fout.write("%5s read %5s \n" % (operAddress[0], address[1]))
  9.     else:
  10.         continue

你知道怎么解决这个问题吗?
提前谢谢你,
M

# 回答1


文件方法'xreadines()'已过时.尝试对文件对象进行迭代:

选择 | 换行 | 行号
  1. fin = open('file_name')
  2. fout = open('file_name1', 'w')
  3.  
  4. for lineStr in fin:
  5.     operAddress = ParseLine(lineStr)
  6.     address = operAddress[2].split('x')
  7.     if (operAddress[1] == 'write'):
  8.         fout.write("%5s write %5s \n" % (operAddress[0], address[1]))
  9.     elif (operAddress[1] == 'read'):
  10.         # print operAddress[0] + address[1]
  11.         fout.write("%5s read %5s \n" % (operAddress[0], address[1]))

除非跳过后面的代码,否则不需要使用ELSE-CONTINUE语句.我猜是你的代码缩进.下次尝试使用代码标签.

标签: python

添加新评论