首页 > 解决方案 > 使用 Python 字典处理文本文件并上传到正在运行的 Web 服务实验室

问题描述

我想编写一个 Python 脚本,它会自动上传反馈,而无需将其转换为字典。脚本现在应该遵循以下结构:

标签: pythondjangohttpflask

解决方案


#! /usr/bin/env python3
import os
import requests
 
dir="/data/feedback/"
url= "http://1.1.1.1/feedback/"
#IMPORTANT: Replace 1.1.1.1 with your
#  Qwiklab's "corpweb" IP Address
 
for file in os.listdir(dir):
    tipos = ["title","name","date","feedback"]
    datos = {}
    lineas = []
    print(file)
    with open(dir+"/"+file,"r") as txtfile:
        x = 0
        for line in txtfile:
            datos[tipos[x]] = line.rstrip('\n')
            x += 1
    print(datos)
    response = requests.post(url,json=datos)

推荐阅读