码迷,mamicode.com
首页 > 编程语言 > 详细

【Python技巧系列】用parse处理文本

时间:2015-09-23 18:53:31      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

parse是python中的一个文本处理工具包,其中包括 findall(), parse(), format() 等等,  findall() 比较常用(对我来说), format(), parse() 互为逆。

 

来看看 parse() 的用法:(代码摘抄自文档)

 1 >>> from parse import *
 2 
 3 >>> parse("It‘s {}, I love it!", "It‘s spam, I love it!")
 4 <Result (spam,) {}>
 5 >>> _[0]
 6 spam
 7 
 8 >>> r = parse("The {} who say {}", "The knights who say Ni!")
 9 >>> print(r)
10 <Result (knights, Ni!) {}>
11 
12 #Dotted names are possible though the application must make additional sense of the result:
13 >>> r = parse("Mmm, {food.type}, I love it!", "Mmm, spam, I love it!")
14 >>> print(r)
15 <Result () {food.type: spam}>
16 >>> print(r.named)
17 {food.type: spam}
18 >>> print(r[food.type])
19 spam
20 
21 
22 >>> ‘‘.join(r.fixed[0] for r in findall(">{}<", "<p>the <b>bold</b> text</p>"))
23 the bold text
24 
25 
26 #If you’re going to use the same pattern to match lots of strings you can compile it once:
27 
28 >>> from parse import compile
29 >>> p = compile("It‘s {}, I love it!")
30 >>> print(p)
31 <Parser "It‘s {}, I love it!">
32 >>> p.parse("It‘s spam, I love it!")
33 <Result (spam,) {}>
34 
35 
36 ##use format specification
37 >>> search(Age: {:d}\n, Name: Rufus\nAge: 42\nColor: red\n)
38 <Result (42,) {}>
39 
40 #will return None if typing does not match
41 >>> parse(Our {:d} {:w} are..., Our 3 weapons are...)
42 <Result (3, weapons) {}>
43 
44 >>> parse(Our {:d} {:w} are..., Our three weapons are...)
45 #none
46 
47 >>> parse(Meet at {:tg}, Meet at 1/2/2011 11:00 PM)
48 <Result (datetime.datetime(2011, 2, 1, 23, 0),) {}>
49 
50 #alignment
51 >>> parse(with {:>} herring, with     a herring)
52 <Result (a,) {}>
53 >>> parse(spam {:^} spam, spam    lovely     spam)
54 <Result (lovely,) {}>
55 
56 #use a dictionary to do some type-convert
57 >>> def shouty(string):
58 ...    return string.upper()
59 ...
60 >>> parse({:shouty} world, hello world, dict(shouty=shouty))
61 <Result (HELLO,) {}>
62 
63 #if the type converter has the pattern attribute
64 >>> def parse_number(text):
65 ...    return int(text)
66 >>> parse_number.pattern = r\d+
67 >>> parse(Answer: {number:Number}, Answer: 42, dict(Number=parse_number))
68 <Result () {number: 42}>
69 >>> _ = parse(Answer: {:Number}, Answer: Alice, dict(Number=parse_number))
70 >>> assert _ is None, "MISMATCH"
71 
72 
73 #use with_pattern(pattern) decorator
74 >>> from parse import with_pattern
75 >>> @with_pattern(r\d+)
76 ... def parse_number(text):
77 ...    return int(text)
78 >>> parse(Answer: {number:Number}, Answer: 42, dict(Number=parse_number))
79 <Result () {number: 42}>

 

【Python技巧系列】用parse处理文本

标签:

原文地址:http://www.cnblogs.com/manqing/p/4832851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!