开发者社区> 问答> 正文

从Jenkins运行时,Python脚本报语法错误

我有一个python脚本来更新Google表格。当我在本地执行该脚本并按预期更新Google工作表时,该脚本运行良好,我希望每3小时自动执行一次。我们正在使用Jenkins进行作业调度,当我尝试从jenkins执行该脚本时,显示语法错误

下面提到了错误和脚本。如何解决该错误的任何建议?

Started by user admin_123
Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/jobs/update_oos_gs/workspace
[workspace] $ /bin/sh -xe /tmp/jenkins6318169151390457385.sh
+ export PYTHONPATH=/home/etl/bi/
+ cd /home/etl/bi/crm
+ python3 -u oos_gs_update.py
  File "oos_gs_update.py", line 22
    r = f"{col_name}{header}:{col_name}{len(col)+header}"
                                                        ^
SyntaxError: invalid syntax
Build step 'Execute shell' marked build as failure
Finished: FAILURE

以下是我的Python脚本,


import os
import sys
import datetime
import psycopg2
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from time import sleep
from utils.config import Configuration as Config
from utils.postgres_helper import get_connection
from utils.utils import get_global_config

sys.path.append('/home/etl/bi/')

GSHEET_CONFIG_SECTION = 'gsheet'
SCOPE =  ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
SHEET_KEY='1Mq7_********y5WtB1R-ZKfz6o'

def update_sheet(sheet, table, columns="ABC", header=4):
    to_update = []
    table = list(zip(*table))
    for col_name, col in zip(columns, table):
        r = f"{col_name}{header}:{col_name}{len(col)+header}"
        cells = sheet.range(r)
        for cell, value in zip(cells, col):
            cell.value = value
        to_update.extend(cells)
    sheet.update_cells(to_update)


cnx_psql =get_connection(get_global_config(), 'pg_dwh')
print('DB connected')
psql_cursor = cnx_psql.cursor()

METADATA_QUERY = '''SELECT sku,product_name,CAST(oos as TEXT) as oos FROM staging.oos_details order by oos DESC;'''
psql_cursor.execute(METADATA_QUERY)
results = psql_cursor.fetchall()
cell_values = (results)

home_dir = os.path.expanduser('~')
config=get_global_config()
gsheet_config_section = GSHEET_CONFIG_SECTION
secret_file_path = os.path.join(home_dir,config.get(gsheet_config_section, 'service_account_credentials'))
creds = ServiceAccountCredentials.from_json_keyfile_name(secret_file_path, scopes=SCOPE)

client = gspread.authorize(creds)
sheet = client.open_by_key(SHEET_KEY).sheet1

#Function Call
update_sheet(sheet, cell_values)

psql_cursor.close()
cnx_psql.close()

展开
收起
安忆333 2019-11-26 10:13:46 1386 0
2 条回答
写回答
取消 提交回答
  • r = f"{col_name}{header}:{col_name}{len(col)+header}"

    len(col)

    int型+str型

    2019-11-26 17:09:56
    赞同 展开评论 打赏
  • 尝试这个:

    r = col_name + str(header) + ':' + col_name + str(len(col)+header)
    

    或者也可以使用其他格式设置方法或将python升级到最新版本

    2019-11-26 10:14:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载