I need to create a POST request that will create an object on my webserver, the object has several attributes that are all extracted from different files, each file has a self explaining name.
I have following file structure
augmentations
team_1
objects
category.txt
description.txt
location.txt
orientation.txt
title.txt
After the loop there should be a dictionary with the attributes from each file that I can use to POST the data as the param in request.post("url", params=parameter)
I have done some reasearch but couldn't manage to find a proper solution to my problem. According to https://www.w3schools.com/python/python_dictionaries.asp you can just add keys to a library like in my code
# -*- coding: utf-8 -*-
import os
import requests
rootdir = 'C:/.../augmentations'
FileType = 'route'
parameter = {}
for subdir, dirs, files in os.walk(rootdir):
for directory in dirs:
for file in files:
if file == 'category.txt' and FileType not in subdir:
f = open(os.path.join(subdir, file), "r", encoding='utf8')
lines = f.readlines()
f.close()
x = ""
for line in lines:
line = line.replace(' ', '')
x += line
parameter["category"] = x
elif file == 'description' and FileType not in subdir:
f = open(os.path.join(subdir, file), "r", encoding='utf8')
lines = f.readlines()
f.close()
x = "" # input
for line in lines:
x += line
parameter["description"] = x
elif file == 'location' and FileType not in subdir:
f = open(os.path.join(subdir, file), "r", encoding='utf8')
lines = f.readlines()
f.close()
lineArray = []
for line in lines:
line = line.replace(' ', '')
lineArray = line.split(',')
parameter["lat"] = lineArray[0]
parameter["lng"] = lineArray[1]
elif file == 'orientation' and FileType not in subdir:
f = open(os.path.join(subdir, file), "r", encoding='utf8')
lines = f.readlines()
f.close()
lineArray = []
for line in lines:
line = line.replace(' ', '')
lineArray = line.split(',')
parameter["x"] = lineArray[0]
parameter["y"] = lineArray[1]
parameter["z"] = lineArray[2]
elif file == 'title' and FileType not in subdir:
f = open(os.path.join(subdir, file), "r", encoding='utf8')
lines = f.readlines()
f.close()
x = "" # input
for line in lines:
x += line
parameter["title"] = x
print(parameter)
The problem is that dont know where that "block" of information ends and the dictionary is ready to be delivered. Where would I need to put the:
r = requests.post("myurl", params=parameters)
The output Im getting from the last print is only {'category': 'culture'} The output should be something like
{'category': 'culture', 'description': 'some description', 'title': 'some title', 'x': '0.123', 'y': '0.891', 'z': '0.871', 'lat': '40.01231', 'lng': '30.0000'}
for every object (around 100)
Solved - EDIT:
the problem was a that I forgot to add the .txt in my elif conditions