Home > Backend Development > Python Tutorial > Python reading and writing Json involves Chinese processing methods

Python reading and writing Json involves Chinese processing methods

高洛峰
Release: 2017-02-23 11:11:41
Original
1270 people have browsed it

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&#39;^article/(?P<main_id>\S+)/$&#39;, views.article, name=&#39;article&#39;),
}
views.py

# coding=utf-8
from django.shortcuts import render
from .models import Tzxy

# Create your views here.


def index(request):
  return render(request, &#39;index.html&#39;)


def add_article(request):
  error = &#39;error&#39;
  if request.method == &#39;POST&#39;:
    # 获取前段request的内容
    main_id = request.POST[&#39;main_id&#39;]
    img_url = request.POST[&#39;img_url&#39;]
    title = request.POST[&#39;title&#39;]
    content = request.POST[&#39;content&#39;]
    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 = &#39;success&#39;
    return render(request, &#39;index.html&#39;, {&#39;error&#39;: error})
  return render(request, &#39;index.html&#39;)


def article(request, main_id):
  article_detial = Tzxy.objects.get(main_id=main_id)
  return render(request, &#39;views.html&#39;, {&#39;content&#39;: 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)
Copy after login

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 == &#39;success&#39; %}
  <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>
Copy after login

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>
Copy after login

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(&#39;utf8&#39;)


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(&#39;http://127.0.0.1:8000/tzxy/article/&#39; + file_name + str(x))
    with open(&#39;/Users/SvenWeng/Desktop/test/&#39; + file_name + str(x) + &#39;.html&#39;, &#39;w&#39;) as f:
      f.write(r.text)
  print &#39;success&#39;


if __name__ == &#39;__main__&#39;:
  tzhtl_name = &#39;tzxy_tzhtl_h_&#39;
  djjyy_name = &#39;tzxy_djjyy_h_&#39;
  tohtml(djjyy_name, 1, 39)
Copy after login

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(&#39;utf8&#39;)

list_json = []

conn = sqlite3.connect(&#39;db.sqlite3&#39;)
c = conn.cursor()
sql = &#39;select * from Tzxy_tzxy&#39;
c.execute(sql)
all_thing = c.fetchall()

for x in all_thing:
  dic_member = {&#39;id&#39;: x[1].split(&#39;_&#39;)[3],
         &#39;img&#39;: x[2],
         &#39;title&#39;: x[3],
         &#39;abstract&#39;: &#39;&#39;}
  list_json.append(dic_member)
conn.close()

final_json = json.dumps(list_json, sort_keys=True, indent=4)
with open(&#39;test.json&#39;, &#39;w&#39;) as f:
  f.write(final_json)
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template