You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`os.listdir("someDir")`| List files, directories in a directory (argument is optional if you wanna list) |
74
74
|`os.path.isdir("someFile")`| Tells you if a path is a dir |
75
-
| `os.path.join(dir, name)` | join some string (file name) with a directory path
76
-
_intelligently_, so it's portable across OSs |
75
+
|`os.path.join(dir, name)`| join some string (file name) with a directory path _intelligently_, so it's portable across OSs |
77
76
77
+
### Handling CSV Files:
78
+
- You'd open and close a CSV file the way you'd do it with any other type of file.
79
+
- We need to **`import csv`** to be able to parse a CSV file with **`csv.reader("openedFile")`** as in:
80
+
```py
81
+
import csv
82
+
83
+
withopen("file.csv") as csvFile:
84
+
reader = csv.reader(csvFile)
85
+
for row in reader:
86
+
print(row)
87
+
```
88
+
- Each row is a list of elements.
89
+
- To generate a csv file, you'd use **`csv.writer("fileTobeWritten").writerows(rows)`** for generating multiple rows. You'd use **`writerow`** instead of `writerows` to generate a single row:
- With **`csv.DictReader(file)`** and **`csv.DictWriter(file)`** you can read and write csv files using dictionaries. Just make sure that such csv files are labeled, meaning the top row has field names.
- Regular expressions in Python can be handled with **`re`** module which include the following great functions:
115
+
- **`re.search(pattern, stringToBeMatched)`**: returns a match object. Search whole string for the pattern.
116
+
- **`re.match(pattern, stringToBeMatched)`**: returns a match object. Only matches if the string starts with the pattern.
117
+
- **`re.findall(pattern, stringToBeMatched)`**: "return all non-overlapping matches of pattern in string, as a list of strings or tupl"
118
+
- The first input to these functions need to be a raw string preceded by an **`**`r`**`** as in **`re.match(r"ca", "california")`**. The reason is to prevent confusion when escaping regex special characters, because strings also have escape characters such as `\n`.
119
+
- The case can be ignored by adding a third argument as in:
- When you surround something in a pattern with parentheses you are capturing a group. The match object your rejex match returns has a function **`groups`** which is a tuple containing these groups. The following example shows how this works:
124
+
```py
125
+
import re
126
+
127
+
result = re.match(r"^(\w+), (\w+)$", "Doe, John")
128
+
print(result.groups()) # prints ('Doe', 'John')
129
+
```
130
+
- Indexes on the match object give us access to each of these groups. Conttinuing witht the example abov:
131
+
- `result[0]` refers to the whole matched pattern `Doe, John`
132
+
- `result[1]` refers to the matched pattern `Doe`
133
+
- `result[2]` refers to the matched pattern `John`
134
+
- Using groups, we can shuffle the pattern and change it around.
135
+
136
+
### Splitting:
137
+
-**`re.split(pattern, stringToBeMatched)`** splits text using a regex. It spits out a list of split elements. If you you wrap separator(s) in parentheses, everything will be included in the output, including the separators as in:
138
+
```py
139
+
import re
140
+
141
+
result = re.split(r"([? ])", "What are you?Nothing")
- Replacements are done with the **`sub`** functions which works as follows:
147
+
```py
148
+
import re
149
+
150
+
result = re.sub(r"\d{3}-\d{3}-\d{4}", "[PHONE NUMBER]", "101-101-2020 called at 22:50")
151
+
# returns '[PHONE NUMBER] called at 22:50'
152
+
```
153
+
- This function returned the targeted string but with specified pattern replaced by the substitution pattern.
154
+
-`sub` also works like the (in)famous `sed`. You can use it along group capturing tot shuffle hings around as in:
155
+
```py
156
+
import re
157
+
158
+
result = re.sub(r"^(\w+), (\w+)$", r"2円1円", "White, Walter")
159
+
```
160
+
161
+
## Managing Data and Processes:
162
+
- This section is about interacting with the OS user and the live interactivity with the OS and processes while they are running!!
163
+
- One great interaction tool is the **`input`** function which accept user input in the terminal. It "prompts" the user to input arguments to the program. One stupid example that illustrate the idiomatic usage of this function is as follows:
164
+
```py
165
+
#!/usr/bin/env python3
166
+
167
+
print("Welcome to our amazing adder!")
168
+
169
+
cont ="y"
170
+
while cont =="y":
171
+
a = b =""
172
+
a =input("Enter first num: ")
173
+
b =input("Enter second num: ")
174
+
print(int(a) +int(b))
175
+
cont =input("Do you wanna perform another operation? [y to continue] ")
176
+
print("Good bye!")
177
+
```
178
+
- The most important thing about the example above is the variable `cont` and how it's used in a loop.
0 commit comments