Search code examples
pythonpandascsvgspread

Import CSV into Google Sheets with local variable in python?


My program downloads a csv from an ftp server, makes modifications to formatting, then I'm currently stuck trying to upload it to the google sheets. I have it working when I select a local file....

Currently when I use the writer variable it says TypeError: '_csv.writer' object is not iterable

with open('test5.csv', 'w') as f:
    # Overwrite the old file with the modified rows
    writer = csv.writer(f)
    writer.writerows(new_rows)**

    print("done!")
    print(str(writer))


gc = gspread.service_account(
    filename='xxx.json')
print("Connected TO GOOGLE")
**gc.import_csv("my-sheet-id", writer)**
print("UPLOADED")

I believe that gspread is looking for a csv file and won't accept variables? Can I use the writer to write a csv file to a local variable instead of to my p? I ask this as I'm looking to make this server sided.


Solution

  • You're passing in writer here:

    gc.import_csv("my-sheet-id", writer)
    

    don't you mean to pass in 'test5.csv'?

    As a side note, it's pretty confusing when you have random stars in your code. You should remove those before posting to SO.