我希望创建一个API端点路由,该路由以JSON格式返回TXT -Python

发布于 2025-02-01 12:04:45 字数 1811 浏览 1 评论 0原文

我是发展的新手,我的问题涉及在我们的路线中创建API端点。 API将用于来自Vuetify UI的帖子。数据将来自我们的MongoDB。我们将为我们的Shell脚本获得一个.txt文件,但必须将其发布为JSON。我认为这些是转换文本文件的步骤: 1)为.txt的行创建列表 2)将每行添加到列表 3)将列表元素加入字符串 4)创建一个带有文件/文件内容的字典并将其转换为JSON,

的步骤代码:

import json

something.txt: an example of the shell script ###
f = open("something.txt")

create a list to put the lines of the file in
file_output = []

add each line of the file to the list
for line in f:
    file_output.append(line)

mashes all of the list elements together into one string
fileoutput2 = ''.join(file_output)
print(fileoutput2)

create a dict with file and file content and then convert to JSON
json_object = {"file": fileoutput2}
json_response = json.dumps(json_object)

print(json_response)
{"file": "Hello\n\nSomething\n\nGoodbye"}

我在下面的基线上有以下代码

@bp_customer.route('/install-setup/<string:customer_id>', methods=['POST'])
def install_setup(customer_id):
    cust = Customer()
    customer = cust.get_customer(customer_id)
    ### example of a series of lines with newline character between them.
    script_string = "Beginning\nof\nscript\n"
    json_object = {"file": script_string}
    json_response = json.dumps(json_object)

   
        get the install shell script content
        replace the values (somebody has already done this)
        attempt to return the below example json_response
    
    return make_response(jsonify(json_response), 200)

这是我当前 在这里:因此,我只需要将其放在帖子中,一旦建立了新的路线,

 onClickScript() {
  console.log("clicked");
  axios
    .get("https://sword-gc-eadsusl5rq-uc.a.run.app/install-setup/")
    .then((resp) => {
      console.log("resp: ", resp.data);
      this.scriptData = resp.data;
    });
},

我很难以正确的方式组合这两个概念。关于我是否处于正确的道路上的任何输入?从我经验丰富的任何人的洞察力?

I'm new to developing and my question(s) involves creating an API endpoint in our route. The api will be used for a POST from a Vuetify UI. Data will come from our MongoDB. We will be getting a .txt file for our shell script but it will have to POST as a JSON. I think these are the steps for converting the text file:
1)create a list for the lines of the .txt
2)add each line to the list
3) join the list elements into a string
4)create a dictionary with the file/file content and convert it to JSON

This is my current code for the steps:

import json

something.txt: an example of the shell script ###
f = open("something.txt")

create a list to put the lines of the file in
file_output = []

add each line of the file to the list
for line in f:
    file_output.append(line)

mashes all of the list elements together into one string
fileoutput2 = ''.join(file_output)
print(fileoutput2)

create a dict with file and file content and then convert to JSON
json_object = {"file": fileoutput2}
json_response = json.dumps(json_object)

print(json_response)
{"file": "Hello\n\nSomething\n\nGoodbye"}

I have the following code for my baseline below that I execute on my button press in the UI

@bp_customer.route('/install-setup/<string:customer_id>', methods=['POST'])
def install_setup(customer_id):
    cust = Customer()
    customer = cust.get_customer(customer_id)
    ### example of a series of lines with newline character between them.
    script_string = "Beginning\nof\nscript\n"
    json_object = {"file": script_string}
    json_response = json.dumps(json_object)

   
        get the install shell script content
        replace the values (somebody has already done this)
        attempt to return the below example json_response
    
    return make_response(jsonify(json_response), 200)

my current Vuetify button press code is here: so I just have to ammend it to a POST and the new route once this is established

 onClickScript() {
  console.log("clicked");
  axios
    .get("https://sword-gc-eadsusl5rq-uc.a.run.app/install-setup/")
    .then((resp) => {
      console.log("resp: ", resp.data);
      this.scriptData = resp.data;
    });
},

I'm having a hard time combining these 2 concepts in the correct way. Any input as to whether I'm on the right path? Insight from anyone who's much more experienced than me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2025-02-08 12:04:45

您走在正确的道路上,但不必要地使事情变得复杂。例如,第一个位可能只是:

import json

with open("something.txt") as f:
    json_response = json.dumps({'file': f.read()})

print(json_response)

并且由于您想通过jsonify ,即使是足够的

with open("something.txt") as f:
    data = {'file': f.read()}

。代码> jsonify 。其余的不够完整,可以提供任何具体的评论,但是基本想法还可以。

如果您有整体工作,则可以转到 https://codereview.stackexchange.com/ 您应该询问一些评论,您应该应该进行一些评论将stackoverflow上的问题限制为有关使某事工作的实际问题。

You're on the right path, but needlessly complicating things a bit. For example, the first bit could be just:

import json

with open("something.txt") as f:
    json_response = json.dumps({'file': f.read()})

print(json_response)

And since you're looking to pass everything through jsonify anyway, even this would suffice:

with open("something.txt") as f:
    data = {'file': f.read()}

Where you can pass data directly through jsonify. The rest of it isn't sufficiently complete to offer any concrete comments, but the basic idea is OK.

If you have a working whole, you could go to https://codereview.stackexchange.com/ to ask for some reviews, you should limit questions on StackOverflow to actual questions about getting something to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文