Last active
May 25, 2018 14:06
-
-
Save kurtis318/857408bc64c2cdff099b53cd4e1a878b to your computer and use it in GitHub Desktop.
Python How-To's and more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a collection of Python How-To's and more. | |
#------------------------------------------------------------------------------- | |
Read a file into a variable: | |
REF: https://stackoverflow.com/questions/3277503/in-python-how-do-i-read-a-file-line-by-line-into-a-list | |
with open('/your/path/file') as f: | |
my_lines = f.readlines() | |
#------------------------------------------------------------------------------- | |
Read a file line by line: | |
REF: https://stackoverflow.com/questions/3277503/in-python-how-do-i-read-a-file-line-by-line-into-a-list | |
with open(...) as f: | |
for line in f: | |
<do something with line> | |
#------------------------------------------------------------------------------- | |
str strip() | |
This function is not mutable (does not modify string object). | |
For example: | |
t = " this string has leading blanks" | |
t.strip() # This is NOT remove leading blanks and change t to contain result. | |
t = t.strip() # This works as desired. | |
#------------------------------------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment