26

私はPythonで作業しており、以下に示すように「_headers」という変数を定義しています

_headers = ('id',
                'recipient_address_1',
                'recipient_address_2',
                'recipient_address_3',
                'recipient_address_4',
                'recipient_address_5',
                'recipient_address_6',
                'recipient_postcode',
                )

これを出力ファイルに書き込むために、次のステートメントを作成しましたが、「AttributeError: 'str' object has no attribute 'write'」というエラーがスローされます。

with open(outfile, 'w') as f:  
            outfile.write(self._headers)  
            print done

助けてください

4

3 に答える 3

2

できるだけスクリプトに近づけるには:

>>> _headers = ('id',
...             'recipient_address_1',
...             'recipient_address_2',
...             'recipient_address_3',
...             'recipient_address_4',
...             'recipient_address_5',
...             'recipient_address_6',
...             'recipient_postcode',
...            )
>>> done = "Operation successfully completed"
>>> with open('outfile', 'w') as f:
...     for line in _headers:
...         f.write(line + "\n")
...     print done
Operation successfully completed
于 2013-09-09T19:28:47.503 に答える