My problem at hand is to get the data from API's and input that data into csv. I'm able to get the data, however outputting the data into csv is where I'm getting the error. Can someone please help.
我手头的问题是从API获取数据并将数据输入到csv中。我可以得到数据,但是输出数据到csv中是我得到错误的地方。有人可以请帮助。
Here is the sample code:
这是样本代码:
import csv,sys
def read_campaign_info(campaigns):
myfile = csv.writer(open("output.csv", "w"))
for insight in reach_insights:
account_id = str(insight[AdsInsights.Field.account_id])
objective = str(insight[AdsInsights.Field.objective])
metrics =[account_id,objective]
wr = csv.writer(myfile,quoting=csv.QUOTE_ALL)
wr.writerows(metrics)
Type of variable metrics
is <class 'list'>
变量指标的类型是
Error that I'm getting is
我得到的误差是。
wr = csv.writer(myfile,quoting=csv.QUOTE_ALL)
TypeError: argument 1 must have a "write" method
1 个解决方案
#1
3
You are passing a csv.writer()
object to csv.writer()
:
您正在将一个csv.writer()对象传递给csv.writer():
myfile = csv.writer(open("output.csv", "w"))
wr = csv.writer(myfile,quoting=csv.QUOTE_ALL)
myfile
is a csv.writer()
object already. Pass your rows to that instead, or remove the csv.writer()
call from the myfile
line (but then move the wr = csv.writer(..)
line out of the loop, you only need to create this once).
myfile是一个csv.writer()对象。将您的行传递给它,或者从myfile行删除csv.writer()调用(然后将wr = csv.writer(..)行移出循环,您只需要创建一次)。
I strongly recommend you use the file object as a context manager so it is closed properly. Also, you want to leave newline handling to the CSV module, so use newline=''
when opening the file:
我强烈建议您使用file对象作为上下文管理器,这样就可以正确地关闭它。另外,您希望将换行处理留给CSV模块,所以在打开文件时使用newline= "
with open("output.csv", "w", newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
for insight in reach_insights:
account_id = insight[AdsInsights.Field.account_id]
objective = insight[AdsInsights.Field.objective]
wr.writerow([account_id, objective])
The csv.writer()
object handles conversions to string for you, so the str()
calls are redundant.
writer()对象处理转换为您的字符串,因此str()调用是多余的。
#1
3
You are passing a csv.writer()
object to csv.writer()
:
您正在将一个csv.writer()对象传递给csv.writer():
myfile = csv.writer(open("output.csv", "w"))
wr = csv.writer(myfile,quoting=csv.QUOTE_ALL)
myfile
is a csv.writer()
object already. Pass your rows to that instead, or remove the csv.writer()
call from the myfile
line (but then move the wr = csv.writer(..)
line out of the loop, you only need to create this once).
myfile是一个csv.writer()对象。将您的行传递给它,或者从myfile行删除csv.writer()调用(然后将wr = csv.writer(..)行移出循环,您只需要创建一次)。
I strongly recommend you use the file object as a context manager so it is closed properly. Also, you want to leave newline handling to the CSV module, so use newline=''
when opening the file:
我强烈建议您使用file对象作为上下文管理器,这样就可以正确地关闭它。另外,您希望将换行处理留给CSV模块,所以在打开文件时使用newline= "
with open("output.csv", "w", newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
for insight in reach_insights:
account_id = insight[AdsInsights.Field.account_id]
objective = insight[AdsInsights.Field.objective]
wr.writerow([account_id, objective])
The csv.writer()
object handles conversions to string for you, so the str()
calls are redundant.
writer()对象处理转换为您的字符串,因此str()调用是多余的。