Spaces:
Sleeping
Sleeping
File size: 1,012 Bytes
bb8ed98 be6ef9f bb8ed98 be6ef9f 0cf3e5e ed7eab5 e1a0fa1 ed7eab5 bb8ed98 3ab3bfe ed7eab5 df66ca0 217c72e f18f04b 0cf3e5e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from flask import Flask, request, jsonify, render_template
import os
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
txt_file = request.files['txt_file']
# Считываем строки из загруженного файла TXT
lines = txt_file.read().decode().split("
")
# Создаем словарь в формате JSON
json_data = {str(index+1): line.strip() for index, line in enumerate(lines)}
# Записываем данные в файл JSON
with open('output.json', 'w') as json_file:
json_file.write(jsonify(json_data).get_data(as_text=True))
return 'Файл успешно обработан и создан файл JSON'
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860))) |