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

时间:2021-10-14 07:59:15

I'm working on Python and have defined a variable called "_headers" as shown below

我正在处理Python,并定义了一个名为“_headers”的变量,如下所示。

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

and in order to write this into an output file, I've written the following statement but it throws me the error "AttributeError: 'str' object has no attribute 'write'"

为了把这个写入输出文件,我写了下面的语句但是它把错误丢给了我"AttributeError: " str "对象没有属性" write "

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

Please help

请帮助

3 个解决方案

#1


17  

You want f.write, not outfile.write...

你想要的。写,不是outfile.write……

outfile is the name of the file as a string. f is the file object.

outfile是文件的名称作为字符串。f是文件对象。

As noted in the comments, file.write expects a string, not a sequence. If you wanted to write data from a sequence, you could use file.writelines. e.g. f.writelines(self._headers). But beware, this doesn't append a newline to each line. You need to do that yourself. :)

如评论所示,文件。写期望字符串,而不是序列。如果要从序列中写入数据,可以使用file.writelines。例如f.writelines(self._headers)。但要注意,这并不会在每行上附加一条新行。你需要自己去做。:)

#2


3  

Assuming that you want 1 header per line, try this:

假设你想要每一行有一个标题,试试这个:

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

#3


1  

To stay as close to your script as possible:

尽量靠近你的剧本:

>>> _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

#1


17  

You want f.write, not outfile.write...

你想要的。写,不是outfile.write……

outfile is the name of the file as a string. f is the file object.

outfile是文件的名称作为字符串。f是文件对象。

As noted in the comments, file.write expects a string, not a sequence. If you wanted to write data from a sequence, you could use file.writelines. e.g. f.writelines(self._headers). But beware, this doesn't append a newline to each line. You need to do that yourself. :)

如评论所示,文件。写期望字符串,而不是序列。如果要从序列中写入数据,可以使用file.writelines。例如f.writelines(self._headers)。但要注意,这并不会在每行上附加一条新行。你需要自己去做。:)

#2


3  

Assuming that you want 1 header per line, try this:

假设你想要每一行有一个标题,试试这个:

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

#3


1  

To stay as close to your script as possible:

尽量靠近你的剧本:

>>> _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