You are on page 1of 39

Python Training Course VII

String and File Processing


String and File Processing

 Format
 Match with Regular Expressions
 File Input/Output
 Structured Text Files
CSV
XML
JSON

2
Format : Old style with %

Multiple data must be


grouped into a tuple.

3
Format : Old style with %

長度最少 10, 靠右 , 不足補空


長度最少 10, 靠左 , 不足補空 長度最少 10, 最多 4, 靠左 , 不足補


白 空白

4
New style formatting with {} and format

New style New style 可指定順


引數也可以是名字

{0} 是 d, {1}
是’ other’

5
New style formatting with {} and format

引數可以是數字可以是名稱

長度最少 10, 靠右 , 不足補空



長度最少 10,> 表示靠右
… ..

長度最少 10,< 表示靠左


… .. 6
New style formatting with {} and format

長度最少 10,^ 表示靠中 , 不足補空


精度不適用整數

: 之後是填充字元
7
Match with Regular Expressions ( 正規表示式 )

compile pattern 加速比



用法

8
Match with Regular Expressions ( 正規表示式 )

match() works only if the pattern search() works if the pattern is anywhere
is at the beginning of the source

9
Match with Regular Expressions ( 正規表示式 )

10
First match with search(), All matches with findall()

? Zero or one

11
Split at matches with split()

Replace at matches with sub()

12
Patterns: special characters

13
Patterns: special characters
which contains 100 printable ASCII characters

digits

alphanumeric character :
digits, letters, or an underscore

whitespaces
14
Pattern specifiers

15
Pattern specifiers

wish or fish

開頭是 I wish

結尾是 fish

16
Pattern specifiers

w or f

one or more runs of w , s , or h

ght followed by a non-alphanumeric

I followed by wish wish preceded by I

17
Using Python’s raw strings

\b 原本應是 re 中的 word boundary


但剛好與 Python 中的 backspace 衝突

Always put an r character


before your regular expression
pattern string.

18
Patterns: specifying match output

When using match() or search() , all


matches are returned from the result
object m as m.group() .

If you use this pattern (?P< name > expr ) ,


it will match expr , saving the match in
group name.

19
A Regular Expression example in the real world

switch terminal command output

(r’\r\n’
)

using paramiko lib. output to 20


練習題

 Book P.170
 Things to Do 7.7~7.11

21
File Input/Output

22
Write a Text File with write()

The write() function returns the number of


File name bytes written. It does not add any spaces
or newlines, as print() does.

最後要記得 close

23
print() or write()
By default, print() adds a space after each
argument and a newline at the end.

Just like using


write()

24
Write file from a large source string

25
Write to an existed file

26
Read a Text File with read()
read 100 characters at a
time and append each
chunk to a poem string
read() 也要
close

27
Read a Text File with readline()

Pythonic

28
Read a Text File with readlines()

The readlines() call reads a


line at a time, and returns a
list of one-line strings.

把 newline 拿

29
Close Files Automatically by Using with

自動關檔

30
練習題

 Book P.215
 Things to Do 8.1~8.2

31
CSV File
Write CSV file
villains file

Read CSV file


into a list of lists

32
csv.DictReader() villains file

Read CSV file


into a list of
dictionaries

33
csv.DictWriter()

header villains file

34
villains file

CSV 檔有表頭就
不必用
fieldnames

35
練習題

 Book P.215
 Things to Do 8.3~8.5

36
XML File into dict.
menu.xml

into
dict.

37
Other XML library
JSON File
menu

into string

string into dict.

39

You might also like