@@ -270,7 +270,7 @@ import re
270
270
< list > = re.split(< regex> , text, maxsplit = 0 ) # Use brackets in regex to keep the matches.
271
271
< Match> = re.search(< regex> , text) # Searches for first occurrence of pattern.
272
272
< Match> = re.match(< regex> , text) # Searches only at the beginning of the text.
273
- < iter > = re.finditer(< regex> , text) # Returns iterator of all matches .
273
+ < iter > = re.finditer(< regex> , text) # Returns all occurrences as match objects .
274
274
```
275
275
276
276
* ** Parameter ` 'flags=re.IGNORECASE' ` can be used with all functions.**
@@ -280,10 +280,11 @@ import re
280
280
281
281
### Match Object
282
282
``` python
283
- < str > = < Match> .group() # Whole match.
284
- < str > = < Match> .group(1 ) # Part in first bracket.
285
- < int > = < Match> .start() # Start index of a match.
286
- < int > = < Match> .end() # Exclusive end index of a match.
283
+ < str > = < Match> .group() # Whole match.
284
+ < str > = < Match> .group(1 ) # Part in first bracket.
285
+ < tuple > = < Match> .groups() # All bracketed parts.
286
+ < int > = < Match> .start() # Start index of a match.
287
+ < int > = < Match> .end() # Exclusive end index of a match.
287
288
```
288
289
289
290
### Special Sequences
@@ -1469,7 +1470,7 @@ desc = 'calculate X to the power of Y'
1469
1470
parser = ArgumentParser(description = desc)
1470
1471
group = parser.add_mutually_exclusive_group()
1471
1472
group.add_argument(' -v' , ' --verbose' , action = ' store_true' )
1472
- group.add_argument(' -q' , ' --quiet' , action = ' store_true' )
1473
+ group.add_argument(' -q' , ' --quiet' , action = ' store_true' )
1473
1474
parser.add_argument(' x' , type = int , help = ' the base' )
1474
1475
parser.add_argument(' y' , type = int , help = ' the exponent' )
1475
1476
args = parser.parse_args()
@@ -1489,12 +1490,13 @@ Table
1489
1490
#### Prints CSV file as ASCII table:
1490
1491
``` python
1491
1492
# $ pip3 install tabulate
1492
- from csv import reader
1493
+ import csv
1493
1494
from tabulate import tabulate
1494
1495
with open (< filename> , encoding = ' utf-8' , newline = ' ' ) as file :
1495
- reader = reader(file , delimiter = ' ;' )
1496
- headers = [a.title() for a in next (reader)]
1497
- print (tabulate(reader, headers))
1496
+ lines = csv.reader(file , delimiter = ' ;' )
1497
+ headers = [a.title() for a in next (lines)]
1498
+ table = tabulate(lines, headers)
1499
+ print (table)
1498
1500
```
1499
1501
1500
1502
@@ -1527,10 +1529,12 @@ Image
1527
1529
``` python
1528
1530
# $ pip3 install pillow
1529
1531
from PIL import Image
1530
- width, height = 100 , 100
1531
- img = Image.new( ' L ' , (width, height), ' white ' )
1532
+ width = 100
1533
+ height = 100
1532
1534
size = width * height
1533
- pixels = [255 * a/ size for a in range (size)]
1535
+ pixels = [255 * i/ size for i in range (size)]
1536
+
1537
+ img = Image.new(' L' , (width, height), ' white' )
1534
1538
img.putdata(pixels)
1535
1539
img.save(' out.png' )
1536
1540
```
@@ -1545,13 +1549,13 @@ img.save('out.png')
1545
1549
1546
1550
Audio
1547
1551
-----
1548
- #### Saves a list of floats with values between 0 and 1 to a WAV file:
1552
+ #### Saves a list of floats with values between -1 and 1 to a WAV file:
1549
1553
``` python
1550
1554
import wave, struct
1551
- samples = [struct.pack(' h' , int ((a - 0.5 ) * 60000 )) for a in < list > ]
1552
- wf = wave.open(< filename > , ' wb' )
1555
+ samples = [struct.pack(' < h' , int (a * 30000 )) for a in < list > ]
1556
+ wf = wave.open(' test.wav ' , ' wb' )
1553
1557
wf.setnchannels(1 )
1554
- wf.setsampwidth(4 )
1558
+ wf.setsampwidth(2 )
1555
1559
wf.setframerate(44100 )
1556
1560
wf.writeframes(b ' ' .join(samples))
1557
1561
wf.close()
0 commit comments