The Processing Method for reading and writing Json in Python involves Chinese characters,
When preparing data for the front-end, you need to convert the data format to the json format. To be honest, it may be really a headache when it comes to Chinese, unless you have a better understanding of the Python encoding rules, otherwise, it would really hurt.
Entire Logic
What we need to deal with is to process some articles, generate multiple html files, and then use json to display the article list, images, summaries, and titles.
Ideas
In order to expand data in the future, there must be a database. My idea is to write a simple webpage for submission input, post it to the background, and then input it to the database, write another page to display the article. After the display effect is correct, write a requests to dynamically crawl all the data to generate one html document. For the final json data, I only need to extract the data from the database to generate an achievement line.
Front end
In fact, the front-end is very simple. Recently, I have been writing web pages, so the front-end is done in minutes. 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 _ article'), 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_article (request): error = 'error' if request. method = 'post': # obtain the content main_id = request in the previous 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. 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)
I wrote a simple form for the template.
Index.html
<!DOCTYPE html>
Displayed page
{% load custom_markdown %}<!DOCTYPE html>
Of course, I used markdown to process some data. For markdown integration, you can go to Django development blog (6)-add markdown support
The following is a small script for data crawling. You need to use the requests module.
# Coding = utf-8import sysimport requestsreload (sys) sys. setdefaultencoding ('utf8') def tohtml (file_name, startpos, endpos): "stores the webpage source code in html format after requesting webpage data, when starting the script, start Django's Server: param file_name: To generate the prefix of the file name, and replace the last digit with the input number: param startpos: Start number: param endpos: number of the end: 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)
You can modify the names as needed.
Generate json
To be honest, the use of json is very simple, and Python supports json well, but it is a bit painful when it comes to Chinese. My code is like 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 to load the generated dictionary information, and then capture all the previously stored data from sqlite. Generate a dictionary of the desired format in a loop and insert the dictionary to the list one by one. Use the json. dumps method provided by Python to convert the data into json format, and then write the data into the file.
The logic seems to be okay, and the implementation is perfect, but at last I opened the json file check and found that all the Chinese characters have become Unicode. This is just a pitfall.
After a rough look, it seems that the content on the Internet is not detailed, and the examples are also very simple. It is not what I want to give directly to Chinese, at last, I had to dig into the official instructions and finally found such a thing.Ensure_ascii = FalseThis method is used when Python is converted to Json, that is
final_json = json.dumps(list_json, sort_keys=True, indent=4, ensure_ascii=False)
After such processing, the written file is normal Chinese.
The above processing method for reading and writing Json in Python involves all the content that I have shared with you. I hope to give you a reference, and I hope you can provide more support to the customer center.