Python reads and writes JSON involves Chinese processing method _python

Source: Internet
Author: User
Tags django server

Today, to help the front of the data, you need to turn the data format into a JSON format, to tell the truth, related to Chinese sometimes really is very painful, unless the coding rules of Python more understanding, or deal with really very egg pain.

The whole logic

What we need to deal with is to process some articles, generate multiple HTML files, and then use JSON to display a list of articles, pictures, summaries, and headings.

Ideas

For future data expansion, that must have a database, my idea is to write a simple Web page for submission input, and then post to the background into the database, and then write a display article page, show the effect is correct, Write a requests dynamic to crawl all the data down to generate an HTML document of one. The final JSON data I just need to extract the data from the database to generate the line.

Front

In fact, the front end of the thing is very simple, has been writing the Web page, so the front end of things in minutes to finish. 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= ' ad D_article '), url (r ' ^article/(?) p<main_id>\s+)/$ ', views.article, name= ' article '), views.py # Coding=utf-8 from django.shortcuts import render fr


Om. Models Import tzxy # Create your views here. def index (Request): Return render (Request, ' index.html ') def add_article (request): Error = ' ERROR ' If Request.meth od = ' POST ': # Get the content of the previous 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_ur L=img_url, Title=title, Content=content, Abstract=abstract) indb.save () E Rror = ' 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 M 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> 

Displayed pages

{% load Custom_markdown%} <! DOCTYPE html>  

Of course, I used markdown to process some of the data. For Markdown integration, you can go to the Django Development blog (vi)--Add markdown Support
The small script to crawl the 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): ""
  "" "
  after the request for Web page data to store the source of the Web page as HTML format, start the script to start the Django server
  :p Aram File_ Name: Generates a prefix for the filename, and the last one replaces the:p Aram Startpos with the incoming number:
  The starting number
  :p Aram Endpos: The ending number
  : 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 naming itself can be modified as needed.

Generate JSON

To be honest, JSON is easy to use and Python is good for JSON, but it's a little bit painful when it comes to Chinese, and my code is 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 load the generated dictionary information, and then grab all the previous data from the SQLite. Loop the data into a dictionary of the format you want, and insert one into the list. Then use the Json.dumps method provided by Python to convert the data into JSON format, and then write to the file.
The logic seems to be fine and it's perfect, but when I finally turned on the JSON file check, I found that all the Chinese were Unicode. This is a pit dad.

Roughly checked, as if the web on the content of the said is not detailed, given the example is very very simple kind, directly to the Chinese, not what I want, and finally can only bite the bullet to see the official instructions, finally found such a thing ensure_ascii=false , take this approach when Python turns json, which is

Final_json = Json.dumps (List_json, Sort_keys=true, indent=4, Ensure_ascii=false)

After this process, writing to the file is normal Chinese.

This python read and write JSON refers to the Chinese processing method is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.