【问题标题】:I am able to define the path in command prompt but unable to define file path in script?我可以在命令提示符中定义路径但无法在脚本中定义文件路径?
【发布时间】:2019-03-19 13:16:18
【问题描述】:

elis_client_example.py

from __future__ import division, print_function

import argparse
import json
import os
import csv
import requests
import polling

DEFAULT_API_URL='https://all.rir.rossum.ai/'

class ElisClient(object):

def __init__(self, secret_key, url=DEFAULT_API_URL):
    self.secret_key = secret_key
    self.url = url
    # we do not use requests.auth.HTTPBasicAuth
    self.headers = {'Authorization': 'secret_key ' + self.secret_key}

def send_document(self, document_path):

    with open(document_path, 'rb') as f:
        content_type = self._content_type(document_path)
        response = requests.post(
            self.url + '/document',
            files={'file': (os.path.basename(document_path), f, content_type)},
            headers=self.headers)
    return json.loads(response.text)

@staticmethod
def _content_type(document_path):
    return 'image/png' if document_path.lower().endswith('.png') else 'application/pdf'

def get_document_status(self, document_id):

    response = requests.get(self.url + '/document/' + document_id, headers=self.headers)
    response_json = json.loads(response.text)
    if response_json['status'] != 'ready':
        response_json = response_json
        #print(response_json)
    return response_json

def get_document(self, document_id, max_retries=30, sleep_secs=5):
    """
    Waits for document via polling.
    """
    def is_done(response_json):
        return response_json['status'] != 'processing'

    return polling.poll(
        lambda: self.get_document_status(document_id),
        check_success=is_done,
        step=sleep_secs,
        timeout=int(round(max_retries * sleep_secs)))

def parse_args():
parser = argparse.ArgumentParser(description='Elis API client example.')
parser.add_argument('document_path', metavar='DOCUMENT_PATH',
                    help='Document path (PDF/PNG)')
parser.add_argument('-s', '--secret-key', help='Secret API key')
parser.add_argument('-u', '--base-url', default=DEFAULT_API_URL, help='Base API URL')

return parser.parse_args()

def main():
args = parse_args()
client = ElisClient(args.secret_key, args.base_url)
#print('Submitting document:', args.document_path)

send_result = client.send_document(args.document_path)
document_id = send_result['id']
#print('Document id:', document_id)
extracted_document = client.get_document(document_id)
#print('Extracted data:')
#Extracted_data =(json.dumps(extracted_document, indent=4))
#print(json.dumps(extracted_document, indent=4))
with open('jfile.json', 'w') as jsonfile:
 json.dump(extracted_document, jsonfile, sort_keys = True, indent = 4,ensure_ascii = False)

if __name__ == '__main__':
main()

运行命令propmt中的代码:python elis_client_example.py input.pdf -s "authentication key" 但是当直接在 python3(script) 中运行代码时,错误就像未定义文档路径一样。 运行命令propmt中的代码:python elis_client_example.py input.pdf -s "authentication key" 但是在 python3(script) 中直接运行代码时,错误就像没有定义文档路径一样。

【问题讨论】:

  • 请有人帮助我...我被困在这里
  • 您是否收到明确的错误?您能否向我们提供您的追溯信息?
  • 我不明白 在 python3(script) 中直接运行代码 是什么意思。 (它是一个 IDE 吗?哪个?IDLE、PythonWin、PyCharm、Jupyter Notebook ......?)你的add_argument 调用说你必须在命令行上提供文件的名称,很明显,当你 直接在python3(脚本)中运行代码你没有这样做。例如,它是 PythonWin 和 PyCharm 中的配置设置。

标签: python api command-prompt filepath


【解决方案1】:

这是一个命令行代码,因此当您在命令提示符下运行相同的代码时,您在执行程序时传递文档名称。但是,当您在任何 ide 中运行时,您实际上是作为“python elis_client_example.py”运行的,而不是像在命令提示符下运行的那样。因此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2015-10-15
    • 2011-11-02
    相关资源
    最近更新 更多