Home > Article > Backend Development > Python reading and writing Json involves Chinese processing methods
Today when preparing data for the front end, we need to convert the data format into json format. To be honest, sometimes it is really a pain in the ass when it comes to Chinese. Unless you have a good understanding of Python coding rules, it will be difficult to process. It hurts.
The whole logic
What we need to process is to process some articles, generate multiple html files, and then use json to display the list of articles, pictures, Abstract and title.
Ideas
In order to expand the data in the future, there must be a database. My idea is to write a simple web page myself as a submission input, and then post it to the background. After entering it into the database, write a page to display the article. After the display effect is correct, write a request to dynamically crawl down all the data to generate html documents one by one. I just need to extract the final json data from the database and generate it.
Front-end
In fact, the front-end stuff is very simple. I have been writing web pages recently, so the front-end stuff can be done in minutes. The code is as follows:
urls.py from django.conf.urls import url, include from . import views urlpatterns = { url(r'^$', views.index, name='index'), url(r'add_article/', views.add_article, name='add_article'), url(r'^article/(?P<main_id>\S+)/$', views.article, name='article'), } views.py # coding=utf-8 from django.shortcuts import render from .models import Tzxy # Create your views here. def index(request): return render(request, 'index.html') def add_article(request): error = 'error' if request.method == 'POST': # 获取前段request的内容 main_id = request.POST['main_id'] img_url = request.POST['img_url'] title = request.POST['title'] content = request.POST['content'] abstract = content[:50] print main_id indb = Tzxy( main_id=main_id, img_url=img_url, title=title, content=content, abstract=abstract ) indb.save() error = 'success' return render(request, 'index.html', {'error': error}) return render(request, 'index.html') def article(request, main_id): article_detial = Tzxy.objects.get(main_id=main_id) return render(request, 'views.html', {'content': article_detial}) models.py from __future__ import unicode_literals from django.db import models from django.contrib import admin class Tzxy(models.Model): main_id = models.CharField(max_length=10) img_url = models.CharField(max_length=50, null=True) title = models.CharField(max_length=50) content = models.TextField() abstract = models.CharField(max_length=200) admin.site.register(Tzxy)
Template I just wrote a simple form
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> </head> <body> <form method="post" action="/tzxy/add_article/"> {% csrf_token %} main_id: <input type="text" name="main_id"><br> img_url: <input type="text" name="img_url"><br> title: <input type="text" name="title"><br> {% if error == 'success' %} <p class="alert alert-success">{{ error }}</p> {% endif %} <textarea name="content" rows="25" style="width: 600px;"></textarea><br> <input type="submit" name="Submit"> </form> </body> </html>
Displayed page
{% load custom_markdown %} <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" /> <meta name="apple-touch-fullscreen" content="yes" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="format-detection" content="telephone=no"> <meta http-equiv="Cache-Control" content="no-store" /> <meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv="Expires" content="0" /> <title>{{ content.title }}</title> <link rel="stylesheet" href="../../css/cssreset.min.css"> <link rel="stylesheet" href="../../css/fx_tzxy_content.min.css"> </head> <body> <p class="page"> <h1>{{ content.title }}</h1> <p class="content"> {{ content.content | custom_markdown | linebreaksbr }} </p> </p> </body> </html>
Of course, I used markdown to process some data. For markdown integration, you can move to "Django Development Blog (6) - Add markdown support"
The small script for crawling data is as follows, you need to use the requests module
# coding=utf-8 import sys import requests reload(sys) sys.setdefaultencoding('utf8') def tohtml(file_name, startpos, endpos): """ 请求网页数据后把网页源码存储为html格式,启动脚本时要先启动Django的Server :param file_name:生成文件名的前缀,最后一位用传入的数字来代替 :param startpos:开始的数字 :param endpos:结束的数字 :return:None """ for x in range(startpos, endpos): r = requests.get('http://127.0.0.1:8000/tzxy/article/' + file_name + str(x)) with open('/Users/SvenWeng/Desktop/test/' + file_name + str(x) + '.html', 'w') as f: f.write(r.text) print 'success' if __name__ == '__main__': tzhtl_name = 'tzxy_tzhtl_h_' djjyy_name = 'tzxy_djjyy_h_' tohtml(djjyy_name, 1, 39)
Some of the names in it can be modified as needed.
Generate json
To be honest, the way to use json is very simple, and Python’s support for json is also very good, but when it comes to Chinese It’s a bit annoying, my code is like this:
# coding=utf-8 import sqlite3 import json import sys reload(sys) sys.setdefaultencoding('utf8') list_json = [] conn = sqlite3.connect('db.sqlite3') c = conn.cursor() sql = 'select * from Tzxy_tzxy' c.execute(sql) all_thing = c.fetchall() for x in all_thing: dic_member = {'id': x[1].split('_')[3], 'img': x[2], 'title': x[3], 'abstract': ''} list_json.append(dic_member) conn.close() final_json = json.dumps(list_json, sort_keys=True, indent=4) with open('test.json', 'w') as f: f.write(final_json)
The code logic is: define an empty list to hold the generated dictionary information, and then from All previously stored data is captured in sqlite. Loop the data to generate a dictionary in the format you want, and insert it into the list one by one. Then use the json.dumps method provided by Python to convert the data into json format, and then write it to the file.
The logic seems to be fine, and the implementation is perfect. However, when I opened the json file to check, I found that all Chinese characters had become Unicode. This is simply a scam.
After a rough search, it seems that the content mentioned on the Internet is not detailed, and the examples given are all very, very simple. Directly given to Chinese, it is not what I want. Yes, in the end I had to bite the bullet and read the official instructions, and finally found something like this ensure_ascii=False. Use this method when converting Python to Json, which is
final_json = json.dumps(list_json, sort_keys=True, indent=4, ensure_ascii=False)
After processing in this way, the written file will be normal Chinese.
The above-mentioned Python reading and writing JSON processing method involving Chinese is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.
For more articles related to Python reading and writing Json involving Chinese processing methods, please pay attention to the PHP Chinese website!