Data storage (JSON file storage, CSV file storage)

1.2 JSON file storage The full name of JSON is JavaScript Object Notation, that is, JavaScript object markup. It repres...
1.2 JSON file storage
1.3 CSV file storage

1.2 JSON file storage

The full name of JSON is JavaScript Object Notation, that is, JavaScript object markup. It represents data through the combination of objects and arrays. It is a lightweight data exchange format with a very high degree of structure.
1.2.1 objects and arrays
Object: it is wrapped in curly braces {} in JavaScript. The data structure is Key value pair structure for. In the object-oriented language, key is the attribute of the object and value is the corresponding value. Key names can be represented by integers and strings. The value can be of any type.
Array: array is the content wrapped by brackets [] in JavaScript. The data structure is ["java", "JavaScript", "python" ]Index structure of. In JavaScript, array is a special data type. It can also use key value pairs like objects, but it still uses index a lot. Again, the value can be of any type.
A JSON object can be written as follows:

[{ "name": "cjh", "sex": "boy", "birthday": "1999-04-02" },{ "name": "mx", "sex": "boy", "birthday": "1998-10-02" }]

This is equivalent to nesting between lists and dictionaries.
1.2.2 read JSON
You can call the loads() method of the JSON library to convert JSON text strings to JSON objects, and you can use the dumps() method to convert JSON objects to text strings.

import json str = ''' [{ "name": "cjh", "sex": "boy", "birthday": "1999-04-02" },{ "name": "mx", "sex": "boy", "birthday": "1998-10-02" }] ''' print(type(str)) data = json.loads(str) print(type(data)) print(data)

The loads() method converts the string to a JSON object. Because the outermost layer is brackets, the final type is a list type.

1.2.3 output JSON
You can use the dumps() method to convert JSON objects to strings.

import json data=[{ "name": "cjh", "sex": "boy", "birthday": "1999-04-02"}] with open('data.json', 'w') as f: f.write(json.dumps(data))

If you want to save the JSON format, you can add another parameter indent to represent the number of indented characters.
If JSON contains Chinese characters, you need to specify the parameter ensure_ascii is False. In addition, the output code of the file should be specified:

with open('data.json', 'w', encoding='utf-8') as f: f.write(json.dumps(data, ensure_ascii=False))

1.3 CSV file storage

1.3.1 write

import csv with open('data.csv', 'w') as f: # Call the writer() method of the csv library to initialize the write object writer = csv.writer(f) # Call the writerow() method to pass in the data of each row writer.writerow(['id', 'name', 'age']) writer.writerow(['1001', 'mike', '20']) writer.writerow(['1002', 'luck', '21']) writer.writerow(['1003', 'chen', '22'])

If you want to modify the separator directly between a column and a column, you can pass in the delimiter parameter in the writer() method.
Write multiple lines using writerows():

import csv with open('data.csv', 'w') as f: # Call the writer() method of the csv library to initialize the write object writer = csv.writer(f) writer.writerow(['id', 'name', 'age']) writer.writerows([['1001', 'mike', '20'], ['1002', 'luck', '21'], ['1003', 'chen', '22']])

Write method of Dictionary:

import csv with open('data.csv', 'w') as f: # Define fields first filednames = ['id', 'name', 'age'] # Call the DictWriter() method to initialize a write object writer = csv.DictWriter(f, filednames=filednames) # Call writeheader() method to write header information writer.writeheader() writer.writerow({'id': '1001', 'name': 'mike', 'age': 20}) writer.writerow({'id': '1002', 'name': 'luck', 'age': 21}) writer.writerow({'id': '1003', 'name': 'chen', 'age': 22})

If you want to append the write, change the file open mode to a.
When writing Chinese content, you need to specify the encoding format for the open() parameter: encoding = 'utf-8'.

1.3.2 reading

import csv with open('data.csv', 'r') as f: reader = csv.reader(f) for row in reader: print(row)

4 June 2020, 09:36 | Views: 9086

Add new comment

For adding a comment, please log in
or create account

0 comments