python 3.第二次执行后子程序错误



我正在使用YouTube API搜索视频,获取一些信息,如channelID, videoID等,并将信息添加到表中。子过程由单击按钮发出的信号触发,并从行编辑中搜索输入。

代码如下:

def searchSend(self):
    search = self.ui.YoutubeSearch.text()
    self.ui.requestView.setRowCount(0)
    def youtube_search(options):
        youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
        developerKey=DEVELOPER_KEY)
        # Call the search.list method to retrieve results matching the specified
        # query term.
        search_response = youtube.search().list(
        q=options.q,
        part="id,snippet",
        maxResults=options.max_results
        ).execute()
        VideoID = []
        ChannelID = []
        VideoName = []
        ChannelName = []
        # Add each result to the appropriate list, and then display the lists of
        # matching videos, channels, and playlists.
        for search_result in search_response.get("items", []):
            if search_result["id"]["kind"] == "youtube#video":
                VideoID.append(search_result["id"]["videoId"])
                ChannelID.append(search_result["snippet"]["channelId"])
                VideoName.append(search_result["snippet"]["title"])
                ChannelName.append(search_result["snippet"]["channelTitle"])
        for item in VideoID:
            rowCount = self.ui.requestView.rowCount()
            self.ui.requestView.insertRow(rowCount)
            self.ui.requestView.setItem(rowCount,0,QtGui.QTableWidgetItem(VideoName[rowCount]))
            self.ui.requestView.setItem(rowCount,1,QtGui.QTableWidgetItem(ChannelName[rowCount]))
            self.ui.requestView.setItem(rowCount,2,QtGui.QTableWidgetItem(VideoID[rowCount]))

    argparser.add_argument("--q", default=str(search))
    argparser.add_argument("--max-results", default=15)
    args = argparser.parse_args()
    try:
        youtube_search(args)
    except HttpError:
        print ("An HTTP error %d occurred:n%s") % (e.resp.status, e.content)

下面是子过程第二次运行时的错误:

Traceback (most recent call last):
  File "D:My DocumentsRequestmainwindow.py", line 112, in searchSend
    argparser.add_argument("--q", default=str(search))
  File "C:Python33libargparse.py", line 1326, in add_argument
    return self._add_action(action)
  File "C:Python33libargparse.py", line 1686, in _add_action
    self._optionals._add_action(action)
  File "C:Python33libargparse.py", line 1530, in _add_action
    action = super(_ArgumentGroup, self)._add_action(action)
  File "C:Python33libargparse.py", line 1340, in _add_action
    self._check_conflict(action)
  File "C:Python33libargparse.py", line 1479, in _check_conflict
    conflict_handler(action, confl_optionals)
  File "C:Python33libargparse.py", line 1488, in _handle_conflict_error
    raise ArgumentError(action, message % conflict_string)
argparse.ArgumentError: argument --q: conflicting option string: --q

谢谢

我认为问题是,当再次运行子过程时,参数已经由argparser添加并存储。

不能再添加参数,因为它与已经存储的参数冲突。

相反,我返回了args值并编辑了其中的'q'值

相关内容

  • 没有找到相关文章

最新更新