2014-04-21 39 views
1

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

我会提供我的代码片段,以及打印输出到屏幕结果:

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”行。不知道为什么这条线上有错误,而不是其他错误。

+0

向我们展示您声明文件句柄的代码'p' – shaktimaan

+0

p = open(“p.log”,“w”) – teachamantofish

回答

3

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

+0

谢谢,我发现我忘记了另一个名为p的变量埋在我的代码中 – teachamantofish

+0

True ,........:D –