AttributeError:“int”对象没有属性“write”

2024-03-17

我正在读取 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”的行。不知道为什么这一行有错误,而其他行没有错误。


在您未发布的代码中的某处有一行覆盖了您的变量p与一个整数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AttributeError:“int”对象没有属性“write” 的相关文章

随机推荐