我該如何解決這個錯誤?
代碼:
import pandas as pd
import seaborn as sns
api_key = 'API_KEY'
channel_id = 'CHANNEL_ID'
youtube = build('youtube','v3', developerKey=api_key)
def get_channel_stats (youtube, channel_id):
request = youtube.channels().list(
part= 'snippet','contentDetails','statistics',id=channel_id)
response = request.execute()
return response
錯誤資訊:
SyntaxError: positional argument follows keyword argument
如何避免此錯誤?我在某個地方犯了一個愚蠢的錯誤,但不知道如何解決它。
uj5u.com熱心網友回復:
您的代碼看起來不錯,您只需要更改發送部分引數的方式。
您需要一個逗號分隔的字串,而不是由逗號分隔的多個字串。
import pandas as pd
import seaborn as sns
api_key = 'API_KEY'
channel_id = 'CHANNEL_ID'
youtube = build('youtube','v3', developerKey=api_key)
def get_channel_stats (youtube, channel_id):
request = youtube.channels().list(
part='snippet,contentDetails,statistics', id=channel_id)
response = request.execute()
return response
uj5u.com熱心網友回復:
假設其余引數youtube.channels().list()的順序正確,您只需要移part = 'snippet'過去即可。決議器希望首先找到所有位置引數(未指定引數名稱的位置引數),因此任何具有<name>=語法的引數都必須位于末尾。
這樣做的原因是許多函式接受*argsand **kwargs,這些函式的意義在于允許任意數量的引數。確保未命名引數分配到正確位置的唯一方法是嚴格控制它們在函式呼叫中的順序和位置。
import pandas as pd
import seaborn as sns
api_key = 'API_KEY'
channel_id = 'CHANNEL_ID'
youtube = build('youtube','v3', developerKey=api_key)
def get_channel_stats (youtube, channel_id):
request = youtube.channels().list(
'contentDetails','statistics', part= 'snippet', id=channel_id)
response = request.execute()
return response
uj5u.com熱心網友回復:
因此,當您在函式呼叫時混合使用關鍵字和位置引數時,實際上會在 python 中引發此錯誤。強文本
您必須以這樣一種方式呼叫函式,即所有位置引數在順序中的任何關鍵字引數之前都排在第一位。
您可以通過更新下面的函式 get_channel_stats 來解決它:
def get_channel_stats (youtube, channel_id):
request = youtube.channels().list(
'contentDetails','statistics', part= 'snippet', id=channel_id)
response = request.execute()
return response
希望它能解決問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/456913.html
