Python: Difference between revisions

From David's Wiki
No edit summary
Line 30: Line 30:
==Regular Expressions (Regex)==
==Regular Expressions (Regex)==
[https://docs.python.org/3/howto/regex.html Reference]<br>
[https://docs.python.org/3/howto/regex.html Reference]<br>
 
<syntaxhighlight lang="python">
 
import re
myReg = re.compile(r'height:(\d+)cm')
myMatch = re.match(myReg, "height:33cm");
print(myMatch[1])
# 33
</syntaxhighlight>


=Libraries=
=Libraries=
==Numpy==
==Numpy==

Revision as of 19:15, 23 September 2019


Installation

Use Anaconda.

Basic Usage

Lambda Function

lambda x: x * 2

Filesystem Read and Write

List all files in a folder

Reference

gazeDir = "Gaze_txt_files"
# List of folders in root folder
gazeFolders = [path.join(gazeDir, x) for x in os.listdir(gazeDir)]
# List of files 2 folders down
gazeFiles = [path.join(x, y) for x in gazeFolders for y in os.listdir(x)]

Read entire text file into a list

[1]

with open('C:/path/numbers.txt') as f:
    lines = f.read().splitlines()

Regular Expressions (Regex)

Reference

import re
myReg = re.compile(r'height:(\d+)cm')
myMatch = re.match(myReg, "height:33cm");
print(myMatch[1])
# 33

Libraries

Numpy