AttributeError:'int'对象没有属性'write'

时间:2014-04-21 19:37:41

标签: python-2.7 file-io

我正在读取csv文件并将结果写入日志文件“p.log”。虽然我在将数据写入文件之前将数据转换为字符串,并且通过多次迭代成功完成,但我一直遇到这样的情况:我最终得到了这个错误: AttributeError:'int'对象没有属性'write'。

我将提供一段代码以及打印在屏幕上的结果:

p=open("p.log","w")

for line in f:#parse lines following the headerline
    line = line.split(",")
    print line, type(str(line))
    p.write('string: '+str(line)+'\n')

['14-04-21 08:30:00.973000', 'EEM06211440.5P', 'EEM', '2014-06-21', '40.50', 'P', '16084', 'AUTO', 'PHLX', '0.82', '10', '0.82', '31', '0.88', '31', '0.06', '0.00', '0.06', 'Y', 'AN', '2', 'AN', '2', 'Bid', '31', '0.0000', '2', 'AN\r\n'] <type 'str'>
['14-04-21 08:30:00.974000', 'MU06211425C', 'MU', '2014-06-21', '25.00', 'C', '0', 'AUTO', 'PSE', '1.57', '11', '1.56', '11', '1.67', '30', '0.11', '0.01', '0.10', 'Y', 'Z', '1', 'IXH', '3', 'Mid', '0', '0', '0', '\r\n'] <type 'str'>
['14-04-21 08:30:00.975000', 'QQQ04251481C', 'QQQ', '2014-04-25', '81.00', 'C', '1585', 'AUTO', 'AMEX', '5.70', '1', '5.20', '82', '5.70', '67', '0.50', '0.50', '0.00', 'Y', 'AINX', '4', 'AINX', '4', 'Ask', '67', '-22.3881', '4', 'AINX\r\n'] <type 'str'>
['14-04-21 08:30:00.976000', 'MPEL05171435.66C', 'MPEL', '2014-05-17', '35.66', 'C', '409', 'AUTO', 'ISE', '2.20', '20', '2.20', '384', '3.10', '20', '0.90', '0.00', '0.90', 'N', 'AI', '2', 'I', '1', 'Bid', '384', '94.7917', '2', 'AI\r\n'] <type 'str'>
['14-04-21 08:30:00.978000', 'AAPL7071914500C', 'AAPL', '2014-07-19', '500.00', 'C', '670', 'AUTO', 'AMEX', '34.85', '3', '34.15', '1', '35.05', '12', '0.90', '0.70', '0.20', 'Y', 'A', '1', 'A', '1', 'Mid', '0', '0', '0', '\r\n'] <type 'str'>
Traceback (most recent call last):
  File "q1.py", line 83, in <module>
    p.write('string: '+str(line)+'\n')
AttributeError: 'int' object has no attribute 'write'

正如您所看到的那样,有几行打印出来,直到打印出“AAPL”。不知道为什么这条线上有错误而不是其他错误。

1 个答案:

答案 0 :(得分:4)

您未发布的代码中的某个位置是用整数覆盖变量p的行。

相关问题