繁体   English   中英

如何将json数据导出到csv或txt文件

[英]How to export json data to csv or txt file

我是 Python 的新毕业生和新手。现在我正在用 Flask 创建基本的 Web 应用程序。 这是我的 Web 应用程序。 这是我的结果。

这是我的代码.py

from flask import Flask, render_template, request, send_file
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
import csv
from datetime import datetime
from io import StringIO
from werkzeug.wrappers import Response
app = Flask(__name__)
@app.route('/')
def index():
   return render_template('index.html')
@app.route('/save',methods = ['POST'])
def save():
   x = dict(request.form.items())
   return "Success %s"%(x)
app.run()

这是我的 index.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<h2>ใบฟอร์ม_โอนเงิน_ทดสอบ_E-mail_Approvel_Project</h2>
</div>
<form id="contact-form" action="/save", method="POST">
    <p>ชื่อ - สกุล:<input type = "text" name = "save1" /></p>
    <p>วันที่:<input type = "date" name = "save2" /></p>
    <p>เลขที่เอกสาร:<input type = "text" name = "save3" /></p>
    <p>รายละเอียด:<input type = "text" name = "save4" /></p>
    <p>จำนวนเงิน:<input type = "text" name = "save5" /></p>

    <p><input type = "submit" value = "submit" /></p>
   </form>
    </div>

    </body>
   </html>

现在: 1.我需要将结果导出到 csv 或 txt 文件。 请告诉我如何得到它。为错误和糟糕的英语水平道歉。

import csv

with open('file.csv', mode='w') as csv_file:
fieldnames = ['header1', 'header2', 'header3']
     writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

     writer.writeheader()
     writer.writerow({'name': 'John Smith', 'dept': 'Accounting','birth_month': 'November'})

这将是一个示例代码,您可以将其放入其中一个函数中。 您将写入名为“file.csv”的 csv 文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM