14

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.

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
MackM
  • 2,906
  • 5
  • 31
  • 45
user4943236
  • 5,914
  • 11
  • 27
  • 40

1 Answers1

12

You are passing a csv.writer() object to 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).

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:

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you for the comment. I modified the code to 'wr = csv.writer(metrics,quoting=csv.QUOTE_ALL)', but still it has the same error. Can you show how to modify my code. Sorry, I'm noob in python – user4943236 Oct 21 '16 at 07:03
  • @user4943236: `metrics` is your row, not a file object. You already created a `csv.writer()`, you don't need to keep creating one in the loop. – Martijn Pieters Oct 21 '16 at 07:08
  • thanks, I modified my code to do the following ` myfile.writerows(metrics) `, now this is the error that I'm getting, any idea how to resolve this > Error: iterable expected, not float – user4943236 Oct 21 '16 at 07:34
  • @user4943236: did you try and modify your code to follow the example in my answer instead? You now appear to have no `csv.writer()` object at all. – Martijn Pieters Oct 21 '16 at 07:40
  • I did and it worked except one issue. The issue was that it was adding one character in one column instead of one complete value in one column. any idea why could be the case – user4943236 Oct 21 '16 at 07:50
  • 1
    @user4943236: ah, I had missed you used `csv.writer.writerows()` (plural), that should be `csv.writer.writerow()` (singular, no `s` at the end). Corrected. – Martijn Pieters Oct 21 '16 at 08:06
  • More generally, you are getting this error because the object passed into `csv.writer()` as `myfile` did not have a `write()` method, which is generally a sign that it was expecting a file-like object. https://stackoverflow.com/questions/4359495/what-is-exactly-a-file-like-object-in-python – MackM Jun 07 '19 at 19:55