Alterna · учебный материал
Text, data and virtual files
Re, fnmatch, csv, json and Python virtual files: take the data by format rather than guessing.
Что в этом материале
- Regular expressions and templates of names
- CSV and JSON: Parser sets format
- Virtual file in the gym
- Quick check
Содержание и основные понятия
Choose a tool on data grammar: string methods for simple format,
re
for the template,
csv
for the table,
json
for the structure.
text and files
10 to 11 grades
EEG: file processing
01
Regular expressions and templates of names
re.findall(pattern, text)
Finds all the intermingling coincidences.
Use the raw strings:
r'\d+'
.
re.fullmatch(pattern, text)
Checks that the template covers the entire line.
More reliable than looking for a piece of the validation line.
fnmatch.fnmatch(name, '*.csv')
Computes the file's name with a simple globe.
It's not a regular expression and it doesn't read the catalogue.
re не всегда нужен
More easily understood to divide between gaps or decimals
split
.
A complex template doesn't improve a simple format.
import
re
text = "id=12; id=7"
print(re.findall(r"id=(\d+)", text))
The group in brackets returns only the numbers after
id=
Without the group, the result would have been all of the overlapping fragments.
02
CSV and JSON: Parser sets format
Format
What Keeps
Tool
Normal text
The lines, the numbers, the words in the sequence.
open(...)
, file cycle,
split
.
CSV
Table lines with the divider and possibly the quotations.
csv.reader
or
csv.DictReader
.
JSON
Lists, dictionaries, numbers, rows and boles of significance.
json.load
, not
eval
.
Security
Don't deal with external JSON through
eval
.
json.load
reads only the right format, and
eval
can do the line as a code.
03
Virtual file in the gym
In this chapter, the virtual field
input.txt
The code can open it with a normal design.
open('input.txt', encoding='utf-8')
This is a learning simulation: the program is isolated from Worker and does not access the computer files.
import
json
with open("input.json", encoding="utf-8") as source:
data = json.load(source)
print(data["name"])
json.load
receive the file stream and return the Python structure. Before reading, the program must know the exact name of the virtual file.
04
Quick check
Which module would correctly read the CSV table, which could contain commas and quotations in the cells?