Python read-write JSON involves Chinese processing methods

Source: Internet
Author: User
Tags django server
Today to help the front-end preparation of data, you need to convert the data format to JSON format, to tell the truth, it is sometimes very painful to relate to Chinese, unless the coding rules of Python more understanding, otherwise it really hurts to deal with the egg.

The entire logic

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

Ideas

For future data expansion, that must have a database, my idea is to write a simple Web page as a submission input, and then post to the background after the entry into the database, and then write a page showing the article, show the effect is correct, Write a requests dynamically put all the data down to generate an HTML document. The final JSON data I just need to extract the data from the database to generate it.

Front

In fact, the front-end thing is very simple, recently has been writing web pages, so the front-end things in minutes to take care of. The code is as follows:

Urls.pyfrom django.conf.urls import URL, includefrom. Import viewsurlpatterns = {URL (r ' ^$ ', Views.index, name= ' index '), URL (r ' add_article/', views.add_article, Name= ' Add_ar Ticle '), url (r ' ^article/(? p<main_id>\s+)/$ ', views.article, name= ' article '),}views.py# coding=utf-8from django.shortcuts Import Renderfrom. Models import tzxy# Create your views Here.def index (Request): Return render (Request, ' index.html ') def Add_ar Ticle (Request): Error = ' ERROR ' if Request.method = = ' POST ': # Gets the contents 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_url=img _url, Title=title, Content=content, Abstract=abstract) indb.save () error = ' suc Cess ' 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.pyfrom __future__ import unicode_literalsfrom django.db import Modelsfrom Django.contrib Import Adminclass 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>

The page to show

{% load Custom_markdown%}<! DOCTYPE html>

Of course, I used the markdown to process some data. For Markdown integration, you can take a "Django Development blog (vi)-Add markdown support"
The small script for crawling data is as follows and needs to be used to the requests module

# coding=utf-8import Sysimport requestsreload (SYS) sys.setdefaultencoding (' UTF8 ') def tohtml (file_name, Startpos, Endpos): "" "  after requesting the Web page data to store the Web page source as HTML format, start the script to start the Django server  :p Aram file_name: Generate the prefix of the file name, the last one with the incoming number instead of  :p Aram Startpos: Starting number  :p Aram Endpos: End 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 names themselves can be modified as needed.

Generate JSON

To tell you the truth, JSON is very simple to use, Python support for JSON is also very good, but it involves a bit of Chinese egg pain, My code is this:

# Coding=utf-8import Sqlite3import jsonimport sysreload (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 that is used to load the generated dictionary information and then capture all the previously stored data from SQLite. The data loop generates the dictionary of the format you want, and one by one is inserted into the list. Then use the Json.dumps method provided by Python to convert the data into JSON format, then write to the file.
Logic seems to be fine, and it's perfect to implement, but in the end I opened the JSON file check and found that all Chinese are Unicode. This is a pit-daddy.

Roughly check, as if the Internet on this piece of content is not detailed, give examples are very very simple kind, directly to the Chinese, not what I want, finally can only bite the bullet to see the official instructions, finally found such a thing ensure_ascii=false , take this method when Python goes to JSON, which is

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

After this processing, writing the file is normal Chinese.

Above this python read and write JSON involves Chinese processing method is small part to share all the content of everyone, I hope to give you a reference, but also hope that we support a lot of topic.alibabacloud.com.

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.