2014-04-21 40 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 –