[英]How do i automatically import a new csv file from my local pc to google sheets every minute?
[英]Import CSV into Google Sheets with local variable in python?
我的程序從 ftp 服務器下載 csv,對格式進行修改,然后我目前正試圖將其上傳到谷歌表格。 當我 select 一個本地文件時,我讓它工作......
目前,當我使用 writer 變量時,它顯示TypeError: '_csv.writer' object is not iterable
import paramiko
import pandas as pd
import csv
import gspread
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname='xxx', port='xxx', username='xxxx',
password='xxxx', allow_agent=False, look_for_keys=False)
curFile = ''
sftp = client.open_sftp()
sftp.put('AJTEST2.csv', 'AJTESTFILE.csv')
with sftp.open('AJTESTFILE.csv', 'rb') as fl:
df = pd.read_csv(fl, sep=',')
print(str(df))
sftp.close()
# MAINPY
new_rows = [] # a holder for our modified rows when we make them
changes = { # a dictionary of changes to make, find 'key' substitue with 'value'
'vehicle_id': 'stock number', # I assume both 'key' and 'value' are strings
'vin': 'vehicle_id',
}
reader = csv.reader(df) # pass the file to our csv reader
for row in reader: # iterate over the rows in the file
new_row = row # at first, just copy the row
for key, value in changes.items(): # iterate over 'changes' dictionary
new_row = [x.replace(key, value)
for x in new_row] # make the substitutions
new_rows.append(new_row) # add the modified rows
**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")
我相信 gspread 正在尋找 csv 文件並且不接受變量? 我可以使用編寫器將 csv 文件寫入局部變量而不是我的 p 嗎? 我問這個是因為我想讓這個服務器站在一邊。
你在這里傳遞writer
:
gc.import_csv("my-sheet-id", writer)
你不是要傳入'test5.csv'嗎?
作為旁注,當您的代碼中有隨機星號時,這會非常令人困惑。 您應該在發布到 SO 之前刪除這些內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.