将文本从一个文件传输到另一个文件

时间:2019-09-12 14:39:12

标签: python

我将dist参数从输入文件传输到结果文件到输入文件中的同一位置时遇到问题 我的代码是:

import requests
import json
import time

with open("query4.txt", "rt") as file:
        data_file = file.read()

with open("result.txt", "w") as f_o:
    for line in data_file.split("\n"):
            for i in range(1):
                    drX, drY, fromX, fromY, dist = line.split(",")

                    url = "https://api.openrouteservice.org/directions?"
                    params = [
                            ["api_key", "my_api_key"],
                            ["coordinates", "%s,%s|%s,%s" % (drY, drX, fromY, fromX)],
                            ["profile", "driving-car"]
                    ]
                    headers = {
                            "Accept": "application/json, application/geo+json,"
                                      "application/gpx+xml, img/png; charset=utf-8"}
                    responce = requests.get(url=url, params=params, headers=headers)
                    print(responce.url)
                    # print(responce.text)
                    result = json.loads(responce.text)
                    print(result)
                    for rows in result["routes"]:
                            print(rows["summary"]["distance"], file=f_o)  # depending on how do you want the result
                            # print(result["routes"])
                            time.sleep(0)

输入示例:

48.94205856,38.48511505,48.94167600,38.49207300,511
46.54586411,30.64417267,46.53338808,30.65455914,1599
50.06436157,31.44526100,50.07415641,31.45929694,1482
50.35911942,30.94097710,50.33576900,30.95166500,2706
50.35837936,30.94162369,50.33576900,30.95166500,2614

现在作为输出,我与摘要json之类的距离

123.2
122.5
221
312.7
212

我想要这个

123.2 125
122.5 122
221 340
312.7 300
212 220

我使用了它,但是似乎不起作用:

with open("query4.txt") as f:
    input_file = f.read()
    with open("result.txt", "w") as f1:
        for line in input_file.split("\n"):
            for line in f:
                dX, dY, fX, fY, dst = line.split(",")
                if "dst" in line:
                    f1.write(line)

1 个答案:

答案 0 :(得分:0)

您只需要将dist变量添加到打印命令。另外,可以对代码进行一些改进:

import requests
import json

url = "https://api.openrouteservice.org/directions?"

headers = {"Accept": "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8"}

with open("query4.txt", "rt") as f_i, open("result.txt", "w") as f_o:

    for line in f_i:
        drX, drY, fromX, fromY, file_dist = line.split(",")

        params = [
                ["api_key", "my_api_key"],
                ["coordinates", "%s,%s|%s,%s" % (drY, drX, fromY, fromX)],
                ["profile", "driving-car"]
        ]

        response = requests.get(url=url, params=params, headers=headers)
        result = json.loads(response.text)

        for rows in result["routes"]:
            f_o.write("{from_api} - {from_file}\n".format(from_api = rows["summary"]["distance"], from_file = file_dist) )
  

如果您对格式部分有所疑问,请看一下:https://realpython.com/python-string-formatting