diff --git a/README.md b/README.md index 6bd07b8..2d7137e 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,7 @@ import re = re.split(, text, maxsplit=0) # Use brackets in regex to keep the matches. = re.search(, text) # Searches for first occurrence of pattern. = re.match(, text) # Searches only at the beginning of the text. - = re.finditer(, text) # Returns iterator of all matches. + = re.finditer(, text) # Returns all occurrences as match objects. ``` * **Parameter `'flags=re.IGNORECASE'` can be used with all functions.** @@ -280,10 +280,11 @@ import re ### Match Object ```python - = .group() # Whole match. - = .group(1) # Part in first bracket. - = .start() # Start index of a match. - = .end() # Exclusive end index of a match. + = .group() # Whole match. + = .group(1) # Part in first bracket. + = .groups() # All bracketed parts. + = .start() # Start index of a match. + = .end() # Exclusive end index of a match. ``` ### Special Sequences @@ -1469,7 +1470,7 @@ desc = 'calculate X to the power of Y' parser = ArgumentParser(description=desc) group = parser.add_mutually_exclusive_group() group.add_argument('-v', '--verbose', action='store_true') -group.add_argument('-q', '--quiet', action='store_true') +group.add_argument('-q', '--quiet', action='store_true') parser.add_argument('x', type=int, help='the base') parser.add_argument('y', type=int, help='the exponent') args = parser.parse_args() @@ -1489,12 +1490,13 @@ Table #### Prints CSV file as ASCII table: ```python # $ pip3 install tabulate -from csv import reader +import csv from tabulate import tabulate with open(, encoding='utf-8', newline='') as file: - reader = reader(file, delimiter=';') - headers = [a.title() for a in next(reader)] - print(tabulate(reader, headers)) + lines = csv.reader(file, delimiter=';') + headers = [a.title() for a in next(lines)] + table = tabulate(lines, headers) + print(table) ``` @@ -1527,10 +1529,12 @@ Image ```python # $ pip3 install pillow from PIL import Image -width, height = 100, 100 -img = Image.new('L', (width, height), 'white') +width = 100 +height = 100 size = width * height -pixels = [255 * a/size for a in range(size)] +pixels = [255 * i/size for i in range(size)] + +img = Image.new('L', (width, height), 'white') img.putdata(pixels) img.save('out.png') ``` @@ -1545,13 +1549,13 @@ img.save('out.png') Audio ----- -#### Saves a list of floats with values between 0 and 1 to a WAV file: +#### Saves a list of floats with values between -1 and 1 to a WAV file: ```python import wave, struct -samples = [struct.pack('h', int((a-0.5)*60000)) for a in ] -wf = wave.open(, 'wb') +samples = [struct.pack('] +wf = wave.open('test.wav', 'wb') wf.setnchannels(1) -wf.setsampwidth(4) +wf.setsampwidth(2) wf.setframerate(44100) wf.writeframes(b''.join(samples)) wf.close()