Bigos lecture from 2024-1119
This commit is contained in:
parent
96c3dc84e3
commit
c7f61c0ab4
49 changed files with 125772 additions and 0 deletions
21
bigos/lecture20241119/Links.txt
Normal file
21
bigos/lecture20241119/Links.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
|
||||||
|
W3Schools Python Read Files
|
||||||
|
============================
|
||||||
|
https://www.w3schools.com/python/python_file_open.asp
|
||||||
|
|
||||||
|
Reading and Writing Files in Python (Guide)
|
||||||
|
===========================================
|
||||||
|
https://realpython.com/read-write-files-python/
|
||||||
|
|
||||||
|
Reading CSV files in Python
|
||||||
|
===========================
|
||||||
|
https://www.programiz.com/python-programming/reading-csv-files
|
||||||
|
|
||||||
|
Reading and Writing CSV Files in Python
|
||||||
|
========================================
|
||||||
|
https://realpython.com/python-csv/
|
||||||
|
|
||||||
|
CSV docs at Python.org
|
||||||
|
=======================
|
||||||
|
https://docs.python.org/3/library/csv.html
|
||||||
|
|
40
bigos/lecture20241119/OReilly csv file overview.txt
Normal file
40
bigos/lecture20241119/OReilly csv file overview.txt
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
Doc from O'Reilly AI Assist
|
||||||
|
=============================
|
||||||
|
|
||||||
|
Python 3 Reads CSV File
|
||||||
|
There are two primary ways to read a CSV file in Python 3: using the built-in csv module and using the popular pandas library.
|
||||||
|
|
||||||
|
Method 1: Using the csv Module
|
||||||
|
Import the csv module: import csv
|
||||||
|
Open the CSV file in read mode: with open('example.csv', 'r') as csvfile:
|
||||||
|
Create a csv.reader object: readCSV = csv.reader(csvfile, delimiter=',')
|
||||||
|
Iterate over the rows: for row in readCSV:
|
||||||
|
Access individual columns using indexing (e.g., row[0] for the first column)
|
||||||
|
Example code:
|
||||||
|
|
||||||
|
import csv
|
||||||
|
|
||||||
|
with open('example.csv', 'r') as csvfile:
|
||||||
|
readCSV = csv.reader(csvfile, delimiter=',')
|
||||||
|
for row in readCSV:
|
||||||
|
print(row) # prints each row as a list
|
||||||
|
print(row[0]) # prints the first column
|
||||||
|
print(row[0], row[1], row[2]) # prints specific columns
|
||||||
|
|
||||||
|
Method 2: Using pandas
|
||||||
|
Import the pandas library: import pandas as pd
|
||||||
|
Read the CSV file into a DataFrame object: df = pd.read_csv('example.csv')
|
||||||
|
Access individual columns using the column name (e.g., df['column_name']) or indexing (e.g., df.iloc[:, 0] for the first column)
|
||||||
|
Example code:
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
df = pd.read_csv('example.csv')
|
||||||
|
print(df) # prints the entire DataFrame
|
||||||
|
print(df['column_name']) # prints a specific column
|
||||||
|
print(df.iloc[:, 0]) # prints the first column
|
||||||
|
|
||||||
|
Key differences
|
||||||
|
The csv module is more lightweight and flexible, but requires more manual effort to parse the CSV file.
|
||||||
|
pandas provides a more convenient and powerful way to work with structured data, but has a larger overhead and requires more memory.
|
||||||
|
Choose the method that best fits your specific use case and requirements.
|
62
bigos/lecture20241119/OReilly file read overview.txt
Normal file
62
bigos/lecture20241119/OReilly file read overview.txt
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
Python File Read Tutorial
|
||||||
|
In this tutorial, we will cover the basics of reading files in Python. We will explore the open() function, file modes, and how to read file contents.
|
||||||
|
|
||||||
|
1. Opening a File
|
||||||
|
To read a file in Python, you need to use the open() function, which takes two arguments: a filename and a file mode. The file mode determines how you intend to interact with the file.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
myfile = open("example.txt", "r")
|
||||||
|
|
||||||
|
In this example, we open a file named “example.txt” in read mode ("r").
|
||||||
|
|
||||||
|
2. File Modes
|
||||||
|
Python provides several file modes:
|
||||||
|
|
||||||
|
"r": Read mode (default). Opens the file for reading only.
|
||||||
|
"w": Write mode. Opens the file for writing only, overwriting any existing content.
|
||||||
|
"r+": Read/Write mode. Opens the file for both reading and writing.
|
||||||
|
"a": Append mode. Opens the file for appending only, without overwriting existing content.
|
||||||
|
"a+": Append and Read mode. Opens the file for both appending and reading.
|
||||||
|
3. Reading File Contents
|
||||||
|
Once you have opened a file, you can read its contents using a loop or by reading a specific number of characters.
|
||||||
|
|
||||||
|
Example (reading each line):
|
||||||
|
|
||||||
|
myfile = open("example.txt", "r")
|
||||||
|
for line in myfile:
|
||||||
|
print(line.strip())
|
||||||
|
myfile.close()
|
||||||
|
|
||||||
|
In this example, we read each line of the file and print it to the console using a for loop. The strip() method removes any trailing newlines or whitespace.
|
||||||
|
|
||||||
|
Example (reading a specific number of characters):
|
||||||
|
|
||||||
|
myfile = open("example.txt", "r")
|
||||||
|
content = myfile.read(10) # read 10 characters
|
||||||
|
print(content)
|
||||||
|
myfile.close()
|
||||||
|
|
||||||
|
In this example, we read the first 10 characters of the file using the read() method.
|
||||||
|
|
||||||
|
4. Closing a File
|
||||||
|
It’s essential to close the file after you’re done reading its contents to free up system resources.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
myfile.close()
|
||||||
|
|
||||||
|
Alternatively, you can use a with statement to ensure the file is closed automatically:
|
||||||
|
|
||||||
|
with open("example.txt", "r") as myfile:
|
||||||
|
for line in myfile:
|
||||||
|
print(line.strip())
|
||||||
|
|
||||||
|
In this example, the with statement ensures the file is closed when the block of code is exited.
|
||||||
|
|
||||||
|
Additional Tips
|
||||||
|
Make sure the file exists and is in the correct location.
|
||||||
|
Be mindful of file encoding and formatting when reading text files.
|
||||||
|
Use the os module to work with file paths and directories.
|
||||||
|
Consider using libraries like csv or json for more advanced file parsing and manipulation.
|
||||||
|
By following this tutorial, you should have a solid understanding of how to read files in Python. Happy coding!
|
32
bigos/lecture20241119/csvfiles/csvline1.py
Normal file
32
bigos/lecture20241119/csvfiles/csvline1.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
|
||||||
|
line = "7/5/2016,123,638.5"
|
||||||
|
|
||||||
|
# split line on commas
|
||||||
|
lineFields = line.split(',')
|
||||||
|
|
||||||
|
print(lineFields)
|
||||||
|
|
||||||
|
# Individual columns from the spreadsheet
|
||||||
|
print(lineFields[0], type(lineFields[0]) )
|
||||||
|
print(lineFields[1], type(lineFields[1]) )
|
||||||
|
print(lineFields[2], type(lineFields[2]) )
|
||||||
|
|
||||||
|
# Convert ISO date --> 20161115 or 2016-1115 or 2016-11-15
|
||||||
|
|
||||||
|
dateFields = lineFields[0].split('/')
|
||||||
|
print(dateFields)
|
||||||
|
|
||||||
|
#print(len(dateFields[0]))
|
||||||
|
#print(len(dateFields[1]))
|
||||||
|
|
||||||
|
if(len(dateFields[0]) == 1):
|
||||||
|
dateFields[0] = "0"+ dateFields[0]
|
||||||
|
|
||||||
|
if(len(dateFields[1]) == 1):
|
||||||
|
dateFields[1] = "0"+ dateFields[1]
|
||||||
|
|
||||||
|
print(dateFields)
|
||||||
|
|
||||||
|
isoDate = dateFields[2] + dateFields[0] + dateFields[1]
|
||||||
|
|
||||||
|
print("ISO Date = ", isoDate)
|
11
bigos/lecture20241119/csvfiles/csvline2.py
Normal file
11
bigos/lecture20241119/csvfiles/csvline2.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# Read a csv file from the current directory
|
||||||
|
# import the csv function library
|
||||||
|
import csv
|
||||||
|
|
||||||
|
filename = "dogs.csv"
|
||||||
|
|
||||||
|
with open(filename,'r') as csvfile:
|
||||||
|
csvData = csv.reader(csvfile,delimiter = ',',quotechar = '"')
|
||||||
|
for row in csvData:
|
||||||
|
print(row)
|
||||||
|
|
8
bigos/lecture20241119/csvfiles/dogs-with-hidden-char.csv
Normal file
8
bigos/lecture20241119/csvfiles/dogs-with-hidden-char.csv
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
DogName,BirthYear,Breed,Color
|
||||||
|
Harry,2007,Chinook,Buff
|
||||||
|
Shenanigans,2008,Chinook,Tawney
|
||||||
|
Mandy,2016,Chinook,Tawney
|
||||||
|
Tanner,2002,Golden Retriever,Tan
|
||||||
|
Rusty,2004,Golden Retriever,Tan
|
||||||
|
Gimli,2022,Chinook,Tawney
|
||||||
|
Yukon Jack,2020,Chinook,Tawney
|
|
8
bigos/lecture20241119/csvfiles/dogs.csv
Normal file
8
bigos/lecture20241119/csvfiles/dogs.csv
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
DogName,BirthYear,Breed,Color
|
||||||
|
Harry,2007,Chinook,Buff
|
||||||
|
Shenanigans,2008,Chinook,Tawney
|
||||||
|
Mandy,2016,Chinook,Tawney
|
||||||
|
Tanner,2002,Golden Retriever,Tan
|
||||||
|
Rusty,2004,Golden Retriever,Tan
|
||||||
|
Gimli,2022,Chinook,Tawney
|
||||||
|
Yukon Jack,2020,Chinook,Tawney
|
|
39
bigos/lecture20241119/csvfiles/hidden-character-dump.txt
Normal file
39
bigos/lecture20241119/csvfiles/hidden-character-dump.txt
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
|
||||||
|
This is from file dogs-with-hidden-char.csv
|
||||||
|
|
||||||
|
Sometimes hidden characters are exported from the spreadsheet program. Run csvline2.py
|
||||||
|
with the filename set to dogs-with-hidden-char.csv
|
||||||
|
|
||||||
|
(base) edbigos@Edwards-MacBook-Pro-4 csvlines % od -bc dogs-with-hidden-char.csv
|
||||||
|
0000000 357 273 277 104 157 147 116 141 155 145 054 102 151 162 164 150
|
||||||
|
357 273 277 D o g N a m e , B i r t h
|
||||||
|
0000020 131 145 141 162 054 102 162 145 145 144 054 103 157 154 157 162
|
||||||
|
Y e a r , B r e e d , C o l o r
|
||||||
|
0000040 015 012 110 141 162 162 171 054 062 060 060 067 054 103 150 151
|
||||||
|
\r \n H a r r y , 2 0 0 7 , C h i
|
||||||
|
0000060 156 157 157 153 054 102 165 146 146 015 012 123 150 145 156 141
|
||||||
|
n o o k , B u f f \r \n S h e n a
|
||||||
|
0000100 156 151 147 141 156 163 054 062 060 060 070 054 103 150 151 156
|
||||||
|
n i g a n s , 2 0 0 8 , C h i n
|
||||||
|
0000120 157 157 153 054 124 141 167 156 145 171 015 012 115 141 156 144
|
||||||
|
o o k , T a w n e y \r \n M a n d
|
||||||
|
0000140 171 054 062 060 061 066 054 103 150 151 156 157 157 153 054 124
|
||||||
|
y , 2 0 1 6 , C h i n o o k , T
|
||||||
|
0000160 141 167 156 145 171 015 012 124 141 156 156 145 162 054 062 060
|
||||||
|
a w n e y \r \n T a n n e r , 2 0
|
||||||
|
0000200 060 062 054 107 157 154 144 145 156 040 122 145 164 162 151 145
|
||||||
|
0 2 , G o l d e n R e t r i e
|
||||||
|
0000220 166 145 162 054 124 141 156 015 012 122 165 163 164 171 054 062
|
||||||
|
v e r , T a n \r \n R u s t y , 2
|
||||||
|
0000240 060 060 064 054 107 157 154 144 145 156 040 122 145 164 162 151
|
||||||
|
0 0 4 , G o l d e n R e t r i
|
||||||
|
0000260 145 166 145 162 054 124 141 156 015 012 107 151 155 154 151 054
|
||||||
|
e v e r , T a n \r \n G i m l i ,
|
||||||
|
0000300 062 060 062 062 054 103 150 151 156 157 157 153 054 124 141 167
|
||||||
|
2 0 2 2 , C h i n o o k , T a w
|
||||||
|
0000320 156 145 171 015 012 131 165 153 157 156 040 112 141 143 153 054
|
||||||
|
n e y \r \n Y u k o n J a c k ,
|
||||||
|
0000340 062 060 062 060 054 103 150 151 156 157 157 153 054 124 141 167
|
||||||
|
2 0 2 0 , C h i n o o k , T a w
|
||||||
|
0000360 156 145 171
|
||||||
|
n e y
|
8
bigos/lecture20241119/datetime/calendar02.py
Normal file
8
bigos/lecture20241119/datetime/calendar02.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
import calendar
|
||||||
|
|
||||||
|
hc = calendar.HTMLCalendar(calendar.MONDAY)
|
||||||
|
str = hc.formatmonth(2024,11)
|
||||||
|
|
||||||
|
|
||||||
|
print(str)
|
16
bigos/lecture20241119/datetime/calendar03.py
Normal file
16
bigos/lecture20241119/datetime/calendar03.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
import calendar
|
||||||
|
|
||||||
|
daysOfWeek = ["Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday", "Sunday"]
|
||||||
|
c = calendar.TextCalendar(calendar.MONDAY)
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
for i in c.itermonthdays(2024,11):
|
||||||
|
if(i > 0):
|
||||||
|
print(i, daysOfWeek[counter % 7])
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
# counter = 0
|
||||||
|
# while(counter < 22):
|
||||||
|
# print(counter % 7, daysOfWeek[counter % 7] )
|
||||||
|
# counter += 1
|
19
bigos/lecture20241119/datetime/calendar1.py
Normal file
19
bigos/lecture20241119/datetime/calendar1.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
import calendar
|
||||||
|
|
||||||
|
c = calendar.TextCalendar(calendar.MONDAY)
|
||||||
|
str = c.formatmonth(2024,11)
|
||||||
|
print(str)
|
||||||
|
|
||||||
|
dayList = list(str)
|
||||||
|
print(dayList)
|
||||||
|
|
||||||
|
calList = str.split("\n")
|
||||||
|
print(calList)
|
||||||
|
|
||||||
|
for line in calList:
|
||||||
|
print(line,len(line))
|
||||||
|
|
||||||
|
for line in calList:
|
||||||
|
print("{:25s}\t{:25s}".format(line,line))
|
||||||
|
|
27
bigos/lecture20241119/datetime/hack.py
Normal file
27
bigos/lecture20241119/datetime/hack.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
import calendar
|
||||||
|
|
||||||
|
months = []
|
||||||
|
|
||||||
|
startMonth = 9
|
||||||
|
monthCounter = startMonth
|
||||||
|
while(monthCounter < startMonth + 3):
|
||||||
|
c = calendar.TextCalendar(calendar.MONDAY)
|
||||||
|
str = c.formatmonth(2024,monthCounter)
|
||||||
|
print(str)
|
||||||
|
calList = str.split("\n")
|
||||||
|
months.append(calList)
|
||||||
|
monthCounter += 1
|
||||||
|
|
||||||
|
print(months)
|
||||||
|
|
||||||
|
print("\n\n")
|
||||||
|
for month in months:
|
||||||
|
print(month,len(month))
|
||||||
|
|
||||||
|
print("\n\n")
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
while(counter < 8):
|
||||||
|
print("{:25s} {:25s} {:25s}".format(months[0][counter],months[1][counter],months[2][counter]))
|
||||||
|
counter += 1
|
14
bigos/lecture20241119/datetime/time01.py
Normal file
14
bigos/lecture20241119/datetime/time01.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import time
|
||||||
|
|
||||||
|
myTime = time.localtime()
|
||||||
|
|
||||||
|
print (time.localtime())
|
||||||
|
|
||||||
|
print(myTime[0], type(myTime[0]))
|
||||||
|
|
||||||
|
print("length = ",len(myTime))
|
||||||
|
|
||||||
|
for myItem in myTime:
|
||||||
|
print(myItem)
|
||||||
|
|
||||||
|
print("The date is {}/{}/{}".format(myTime[1],myTime[2],myTime[0]) )
|
9
bigos/lecture20241119/datetime/timeelapsed.py
Normal file
9
bigos/lecture20241119/datetime/timeelapsed.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import time
|
||||||
|
|
||||||
|
# https://stackoverflow.com/questions/7370801/how-do-i-measure-elapsed-time-in-python
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
print("hello")
|
||||||
|
time.sleep(4)
|
||||||
|
end = time.time()
|
||||||
|
print(end - start)
|
88799
bigos/lecture20241119/fileio_census/all-last.txt
Normal file
88799
bigos/lecture20241119/fileio_census/all-last.txt
Normal file
File diff suppressed because it is too large
Load diff
7
bigos/lecture20241119/fileio_census/census-data-info.txt
Normal file
7
bigos/lecture20241119/fileio_census/census-data-info.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
The columns in the census data correspond to:
|
||||||
|
|
||||||
|
Column Data
|
||||||
|
1 A "Name"
|
||||||
|
2 Frequency in percent
|
||||||
|
3 Cumulative Frequency in percent
|
||||||
|
4 Rank
|
4275
bigos/lecture20241119/fileio_census/female-first.csv
Normal file
4275
bigos/lecture20241119/fileio_census/female-first.csv
Normal file
File diff suppressed because it is too large
Load diff
4275
bigos/lecture20241119/fileio_census/female-first.txt
Normal file
4275
bigos/lecture20241119/fileio_census/female-first.txt
Normal file
File diff suppressed because it is too large
Load diff
28
bigos/lecture20241119/fileio_census/file-read01.py
Normal file
28
bigos/lecture20241119/fileio_census/file-read01.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
## ============================ One time initializations ========================
|
||||||
|
fileName = "testdata.txt"
|
||||||
|
|
||||||
|
|
||||||
|
## ============================ Open and process file(s) ========================
|
||||||
|
|
||||||
|
try:
|
||||||
|
inFile = open(fileName,'r')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
runFlag = True
|
||||||
|
while(runFlag): # Process while true
|
||||||
|
# print(count,line.rstrip("\n"))
|
||||||
|
line = inFile.readline()
|
||||||
|
if line == "":
|
||||||
|
runFlag == False
|
||||||
|
else:
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
print(count,line)
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
except:
|
||||||
|
print("Some error occurred. Ending here")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
inFile.close()
|
28
bigos/lecture20241119/fileio_census/file-read02.py
Normal file
28
bigos/lecture20241119/fileio_census/file-read02.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
## ============================ One time initializations ========================
|
||||||
|
fileName = "testdata.txt"
|
||||||
|
|
||||||
|
|
||||||
|
## ============================ Open and process file(s) ========================
|
||||||
|
|
||||||
|
try:
|
||||||
|
inFile = open(fileName,'r')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
runFlag = True
|
||||||
|
while(runFlag): # Process while true
|
||||||
|
# print(count,line.rstrip("\n"))
|
||||||
|
line = inFile.readline()
|
||||||
|
if not (line == ""):
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
print(count,line)
|
||||||
|
count = count + 1
|
||||||
|
else:
|
||||||
|
runFlag == False
|
||||||
|
|
||||||
|
except:
|
||||||
|
print("Some error occurred. Ending here")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
inFile.close()
|
28
bigos/lecture20241119/fileio_census/file-read03.py
Normal file
28
bigos/lecture20241119/fileio_census/file-read03.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
## ============================ One time initializations ========================
|
||||||
|
fileName = "testdata.txt"
|
||||||
|
|
||||||
|
|
||||||
|
## ============================ Open and process file(s) ========================
|
||||||
|
|
||||||
|
try:
|
||||||
|
inFile = open(fileName,'r')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
runFlag = True
|
||||||
|
while(1): # Process while true
|
||||||
|
|
||||||
|
line = inFile.readline()
|
||||||
|
if line == "": # Exit on empty line == end of file
|
||||||
|
break
|
||||||
|
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
print(count,line)
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
except:
|
||||||
|
print("Some error occurred. Ending here")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
inFile.close()
|
35
bigos/lecture20241119/fileio_census/file-read04.py
Normal file
35
bigos/lecture20241119/fileio_census/file-read04.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
import re
|
||||||
|
|
||||||
|
## ============================ One time initializations ========================
|
||||||
|
fileName = "female-first.txt"
|
||||||
|
outFileName = "female-first.csv"
|
||||||
|
|
||||||
|
|
||||||
|
## ============================ Open and process file(s) ========================
|
||||||
|
|
||||||
|
try:
|
||||||
|
inFile = open(fileName,'r')
|
||||||
|
outFile = open(outFileName,'w')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
runFlag = True
|
||||||
|
while(1): # Process while true
|
||||||
|
|
||||||
|
line = inFile.readline()
|
||||||
|
if line == "": # Exit on empty line == end of file
|
||||||
|
break
|
||||||
|
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
line = line.lstrip().rstrip()
|
||||||
|
print(count,line)
|
||||||
|
csvLine = re.sub(' +',',',line)
|
||||||
|
print("\t",csvLine)
|
||||||
|
outFile.write(csvLine + "\n")
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
except:
|
||||||
|
print("Some error occurred. Ending here")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
inFile.close()
|
25
bigos/lecture20241119/fileio_census/fileread01.py
Normal file
25
bigos/lecture20241119/fileio_census/fileread01.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
# See https://stackabuse.com/read-a-file-line-by-line-in-python/
|
||||||
|
# for more information
|
||||||
|
# Just try reading it line by line on the first attempt.
|
||||||
|
|
||||||
|
fileName = "testdata.txt"
|
||||||
|
try:
|
||||||
|
inFile = open(fileName,'r')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
line = inFile.readline()
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
while(line): # Read line and place in variable named line
|
||||||
|
# print(count,line.rstrip("\n"))
|
||||||
|
print(count,line)
|
||||||
|
line = inFile.readline()
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
except:
|
||||||
|
print("Some error occurred. Ending here")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
inFile.close()
|
38
bigos/lecture20241119/fileio_census/get-firstname.py
Normal file
38
bigos/lecture20241119/fileio_census/get-firstname.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
## ============================ One time initializations ========================
|
||||||
|
fileName = "male-first.txt"
|
||||||
|
|
||||||
|
nameToSearch = input("Enter a first name: ")
|
||||||
|
nameToSearch = nameToSearch.upper()
|
||||||
|
|
||||||
|
## ============================ Open and process file(s) ========================
|
||||||
|
|
||||||
|
try:
|
||||||
|
inFile = open(fileName,'r')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
runFlag = True
|
||||||
|
while(1): # Process while true
|
||||||
|
|
||||||
|
line = inFile.readline()
|
||||||
|
if line == "": # Exit on empty line == end of file
|
||||||
|
break
|
||||||
|
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
# print(count,line)
|
||||||
|
fieldList = line.split(" ")
|
||||||
|
# print(fieldList)
|
||||||
|
print(fieldList[0],len(fieldList[0]))
|
||||||
|
|
||||||
|
if nameToSearch == fieldList[0] :
|
||||||
|
print("Matched {} = {} at position {}".format(nameToSearch,fieldList[0],count))
|
||||||
|
break
|
||||||
|
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
except:
|
||||||
|
print("Some error occurred. Ending here")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
inFile.close()
|
34
bigos/lecture20241119/fileio_census/get-name-with-spaces.py
Normal file
34
bigos/lecture20241119/fileio_census/get-name-with-spaces.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
## ============================ One time initializations ========================
|
||||||
|
fileName = "female-first.txt"
|
||||||
|
|
||||||
|
|
||||||
|
## ============================ Open and process file(s) ========================
|
||||||
|
|
||||||
|
try:
|
||||||
|
inFile = open(fileName,'r')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
runFlag = True
|
||||||
|
while(1): # Process while true
|
||||||
|
|
||||||
|
line = inFile.readline()
|
||||||
|
if line == "": # Exit on empty line == end of file
|
||||||
|
break
|
||||||
|
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
print(count,line)
|
||||||
|
line = line.replace(" "," ")
|
||||||
|
print("\t",count,line)
|
||||||
|
fieldList = line.split(" ")
|
||||||
|
print(fieldList)
|
||||||
|
for field in fieldList:
|
||||||
|
print(field)
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
except:
|
||||||
|
print("Some error occurred. Ending here")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
inFile.close()
|
1219
bigos/lecture20241119/fileio_census/male-first.txt
Normal file
1219
bigos/lecture20241119/fileio_census/male-first.txt
Normal file
File diff suppressed because it is too large
Load diff
8
bigos/lecture20241119/fileio_dogs/dogs.csv
Normal file
8
bigos/lecture20241119/fileio_dogs/dogs.csv
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
DogName,BirthYear,Breed,Color
|
||||||
|
Harry,2007,Chinook,Buff
|
||||||
|
Shenanigans,2008,Chinook,Tawney
|
||||||
|
Mandy,2016,Chinook,Tawney
|
||||||
|
Tanner,2002,Golden Retriever,Tan
|
||||||
|
Rusty,2004,Golden Retriever,Tan
|
||||||
|
Gimli,2022,Chinook,Tawney
|
||||||
|
Yukon Jack,2020,Chinook,Tawney
|
|
BIN
bigos/lecture20241119/fileio_dogs/dogs.xlsx
Normal file
BIN
bigos/lecture20241119/fileio_dogs/dogs.xlsx
Normal file
Binary file not shown.
23
bigos/lecture20241119/fileio_dogs/fileread01.py
Normal file
23
bigos/lecture20241119/fileio_dogs/fileread01.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# See https://stackabuse.com/read-a-file-line-by-line-in-python/
|
||||||
|
# for more information
|
||||||
|
# Just try reading it line by line on the first attempt.
|
||||||
|
filename = "testxdata.txt"
|
||||||
|
try:
|
||||||
|
inFile = open(filename,'r')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
line = inFile.readline()
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
while(line): # Read line and place in variable named line
|
||||||
|
# print(count,line.rstrip("\n"))
|
||||||
|
print(count,line)
|
||||||
|
line = inFile.readline()
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
count = count + 1
|
||||||
|
except:
|
||||||
|
print(f"The file {filename} cannot be accessed.")
|
||||||
|
sys.exit(-1)
|
||||||
|
|
29
bigos/lecture20241119/fileio_dogs/fileread022.py
Normal file
29
bigos/lecture20241119/fileio_dogs/fileread022.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
# See https://stackabuse.com/read-a-file-line-by-line-in-python/
|
||||||
|
# for more information
|
||||||
|
# Just try reading it line by line on the first attempt.
|
||||||
|
try:
|
||||||
|
inFile = open("dogs.csv",'r')
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
line = inFile.readline()
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
# process header
|
||||||
|
columnNames = line.split(",")
|
||||||
|
print(columnNames)
|
||||||
|
while(line): # Read line and place in variable named line
|
||||||
|
# print(count,line.rstrip("\n"))
|
||||||
|
print(count,line)
|
||||||
|
columns = line.split(",")
|
||||||
|
# print(columns)
|
||||||
|
counter = 0
|
||||||
|
while(counter < 4):
|
||||||
|
print("\t{:12s} {:>20s}".format(columnNames[counter],columns[counter]) )
|
||||||
|
counter = counter + 1
|
||||||
|
line = inFile.readline()
|
||||||
|
line = line.rstrip("\n")
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
finally:
|
||||||
|
inFile.close()
|
22
bigos/lecture20241119/fileio_dogs/filewrite01.py
Normal file
22
bigos/lecture20241119/fileio_dogs/filewrite01.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
# See https://stackabuse.com/read-a-file-line-by-line-in-python/
|
||||||
|
# for more information
|
||||||
|
# Just try reading it line by line on the first attempt.
|
||||||
|
|
||||||
|
sampleData = ["Harry", "Colorado", "Shenanigans", "Mandy", "Rusty"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
outFile = open("testdatawrite.txt",'w')
|
||||||
|
|
||||||
|
for dog in sampleData:
|
||||||
|
print(dog)
|
||||||
|
outFile.writelines(dog+"\n")
|
||||||
|
|
||||||
|
except:
|
||||||
|
print("File write error!")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
# When I get to the end of the file.
|
||||||
|
#finally:
|
||||||
|
outFile.close()
|
4
bigos/lecture20241119/fileio_dogs/testdata.txt
Normal file
4
bigos/lecture20241119/fileio_dogs/testdata.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
Harry,2007
|
||||||
|
Shenanigans,2010
|
||||||
|
Katrina,2005
|
||||||
|
Mandy,2016
|
5
bigos/lecture20241119/fileio_dogs/testdatawrite.txt
Normal file
5
bigos/lecture20241119/fileio_dogs/testdatawrite.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
Harry
|
||||||
|
Colorado
|
||||||
|
Shenanigans
|
||||||
|
Mandy
|
||||||
|
Rusty
|
11
bigos/lecture20241119/files_directory/directory1.py
Normal file
11
bigos/lecture20241119/files_directory/directory1.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
myPath = "./"
|
||||||
|
|
||||||
|
files = os.listdir(myPath)
|
||||||
|
|
||||||
|
files.sort()
|
||||||
|
|
||||||
|
print(files)
|
||||||
|
|
||||||
|
|
25
bigos/lecture20241119/files_directory/directory2.py
Normal file
25
bigos/lecture20241119/files_directory/directory2.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
myPath = "./"
|
||||||
|
|
||||||
|
files = os.listdir(myPath)
|
||||||
|
|
||||||
|
files.sort()
|
||||||
|
|
||||||
|
print(files)
|
||||||
|
|
||||||
|
textFiles = []
|
||||||
|
|
||||||
|
for myFile in files:
|
||||||
|
print(myFile)
|
||||||
|
fileParts = myFile.split(".")
|
||||||
|
print(fileParts)
|
||||||
|
if(len(fileParts) > 2):
|
||||||
|
print(len(fileParts))
|
||||||
|
filePartsLength = len(fileParts)
|
||||||
|
if fileParts[filePartsLength-1] == "txt":
|
||||||
|
print("Text file!")
|
||||||
|
textFiles.append(myFile)
|
||||||
|
|
||||||
|
|
||||||
|
print("The text files are: ",textFiles)
|
16
bigos/lecture20241119/files_directory/dirwalk4.py
Normal file
16
bigos/lecture20241119/files_directory/dirwalk4.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import glob
|
||||||
|
import os
|
||||||
|
|
||||||
|
x = glob.glob("*.txt")
|
||||||
|
|
||||||
|
print(x)
|
||||||
|
|
||||||
|
x = glob.glob("*.py")
|
||||||
|
|
||||||
|
print(x)
|
||||||
|
|
||||||
|
for f in x:
|
||||||
|
print(f)
|
||||||
|
print(os.path.isfile("./"+f))
|
||||||
|
|
||||||
|
|
5
bigos/lecture20241119/files_directory/glob.py
Normal file
5
bigos/lecture20241119/files_directory/glob.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import glob
|
||||||
|
|
||||||
|
files = glob.glob("./")
|
||||||
|
|
||||||
|
print(files)
|
4357
bigos/lecture20241119/files_pc_inventory/00-16-76-b2-b2-71.csv
Executable file
4357
bigos/lecture20241119/files_pc_inventory/00-16-76-b2-b2-71.csv
Executable file
File diff suppressed because it is too large
Load diff
4466
bigos/lecture20241119/files_pc_inventory/00-16-76-c0-3e-a7.csv
Executable file
4466
bigos/lecture20241119/files_pc_inventory/00-16-76-c0-3e-a7.csv
Executable file
File diff suppressed because it is too large
Load diff
4455
bigos/lecture20241119/files_pc_inventory/00-16-76-c0-40-23.csv
Executable file
4455
bigos/lecture20241119/files_pc_inventory/00-16-76-c0-40-23.csv
Executable file
File diff suppressed because it is too large
Load diff
4362
bigos/lecture20241119/files_pc_inventory/00-16-76-c0-40-d3.csv
Executable file
4362
bigos/lecture20241119/files_pc_inventory/00-16-76-c0-40-d3.csv
Executable file
File diff suppressed because it is too large
Load diff
4439
bigos/lecture20241119/files_pc_inventory/00-16-76-c0-46-e7.csv
Executable file
4439
bigos/lecture20241119/files_pc_inventory/00-16-76-c0-46-e7.csv
Executable file
File diff suppressed because it is too large
Load diff
4364
bigos/lecture20241119/files_pc_inventory/00-19-d1-6e-d3-db.csv
Executable file
4364
bigos/lecture20241119/files_pc_inventory/00-19-d1-6e-d3-db.csv
Executable file
File diff suppressed because it is too large
Load diff
3
bigos/lecture20241119/files_pc_inventory/README.txt
Normal file
3
bigos/lecture20241119/files_pc_inventory/README.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
These are the outputs of an (old) PC inventory program.
|
||||||
|
They create a csv file of the PC's configuration which saved under the filename derived from the
|
||||||
|
network address.
|
13
bigos/lecture20241119/files_pc_inventory/csvInventoryList.py
Normal file
13
bigos/lecture20241119/files_pc_inventory/csvInventoryList.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# Read a csv file from the current directory
|
||||||
|
# import the csv function library
|
||||||
|
import csv
|
||||||
|
|
||||||
|
lineCounter = 0
|
||||||
|
filename = "00-16-76-b2-b2-71.csv"
|
||||||
|
|
||||||
|
with open(filename,'r') as csvfile:
|
||||||
|
csvData = csv.reader(csvfile,delimiter = ',',quotechar = '"')
|
||||||
|
for row in csvData:
|
||||||
|
lineCounter += 1 # Sharthand for LineCounter = LineCounter + 1
|
||||||
|
print(f"{lineCounter:5d} {row}")
|
||||||
|
|
8
bigos/lecture2024112/try01.py
Normal file
8
bigos/lecture2024112/try01.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
data1 = float(input("Enter the dividend "))
|
||||||
|
|
||||||
|
data2 = float(input("Enter the divisor "))
|
||||||
|
|
||||||
|
quotient = data1/data2
|
||||||
|
|
||||||
|
print(f"{data1}/{data2} = {quotient}")
|
13
bigos/lecture2024112/try02.py
Normal file
13
bigos/lecture2024112/try02.py
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
data1 = float(input("Enter the dividend "))
|
||||||
|
|
||||||
|
data2 = float(input("Enter the divisor "))
|
||||||
|
|
||||||
|
try:
|
||||||
|
quotient = data1/data2
|
||||||
|
print(f"{data1}/{data2} = {quotient}")
|
||||||
|
except:
|
||||||
|
print(f"Invalid value. Division by zero is not allowed.")
|
||||||
|
sys.exit(-1)
|
||||||
|
|
20
bigos/lecture2024112/try03.py
Normal file
20
bigos/lecture2024112/try03.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
try:
|
||||||
|
data1 = float(input("Enter the dividend "))
|
||||||
|
except:
|
||||||
|
print(f"Invalid value for the dividend ")
|
||||||
|
sys.exit(-2)
|
||||||
|
try:
|
||||||
|
data2 = float(input("Enter the divisor "))
|
||||||
|
except:
|
||||||
|
print(f"Invalid value for the divisor ")
|
||||||
|
sys.exit(-3)
|
||||||
|
|
||||||
|
try:
|
||||||
|
quotient = data1/data2
|
||||||
|
print(f"{data1}/{data2} = {quotient}")
|
||||||
|
except:
|
||||||
|
print(f"Invalid value. Division by zero is not allowed.")
|
||||||
|
sys.exit(-1)
|
||||||
|
|
19
bigos/lecture2024112/try04.py
Normal file
19
bigos/lecture2024112/try04.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def getFloatValue(message):
|
||||||
|
try:
|
||||||
|
data1 = float(input(message))
|
||||||
|
except:
|
||||||
|
print(f"Invalid value for a floating point number. ")
|
||||||
|
sys.exit(-2)
|
||||||
|
return data1
|
||||||
|
|
||||||
|
data1 = getFloatValue("Enter value for the dividend ")
|
||||||
|
data2 = getFloatValue("Enter value for the divisor ")
|
||||||
|
|
||||||
|
try:
|
||||||
|
quotient = data1/data2
|
||||||
|
print(f"{data1}/{data2} = {quotient}")
|
||||||
|
except:
|
||||||
|
print(f"Invalid value. Division by zero is not allowed.")
|
||||||
|
sys.exit(-1)
|
Loading…
Add table
Add a link
Reference in a new issue