Bigos lecture from 2024-1119

main
Edward Bigos 2024-11-20 10:14:40 -05:00
parent 96c3dc84e3
commit c7f61c0ab4
49 changed files with 125772 additions and 0 deletions

View 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

View 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.

View 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
Its essential to close the file after youre 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!

View 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)

View 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)

View 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
1 DogName BirthYear Breed Color
2 Harry 2007 Chinook Buff
3 Shenanigans 2008 Chinook Tawney
4 Mandy 2016 Chinook Tawney
5 Tanner 2002 Golden Retriever Tan
6 Rusty 2004 Golden Retriever Tan
7 Gimli 2022 Chinook Tawney
8 Yukon Jack 2020 Chinook Tawney

View 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
1 DogName BirthYear Breed Color
2 Harry 2007 Chinook Buff
3 Shenanigans 2008 Chinook Tawney
4 Mandy 2016 Chinook Tawney
5 Tanner 2002 Golden Retriever Tan
6 Rusty 2004 Golden Retriever Tan
7 Gimli 2022 Chinook Tawney
8 Yukon Jack 2020 Chinook Tawney

View 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

View File

@ -0,0 +1,8 @@
import calendar
hc = calendar.HTMLCalendar(calendar.MONDAY)
str = hc.formatmonth(2024,11)
print(str)

View 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

View 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))

View 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

View 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]) )

View 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)

File diff suppressed because it is too large Load Diff

View 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

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View 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()

View 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()

View 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()

View 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()

View 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()

View 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()

View 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()

File diff suppressed because it is too large Load Diff

View 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
1 DogName BirthYear Breed Color
2 Harry 2007 Chinook Buff
3 Shenanigans 2008 Chinook Tawney
4 Mandy 2016 Chinook Tawney
5 Tanner 2002 Golden Retriever Tan
6 Rusty 2004 Golden Retriever Tan
7 Gimli 2022 Chinook Tawney
8 Yukon Jack 2020 Chinook Tawney

Binary file not shown.

View 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)

View 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()

View 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()

View File

@ -0,0 +1,4 @@
Harry,2007
Shenanigans,2010
Katrina,2005
Mandy,2016

View File

@ -0,0 +1,5 @@
Harry
Colorado
Shenanigans
Mandy
Rusty

View File

@ -0,0 +1,11 @@
import os
myPath = "./"
files = os.listdir(myPath)
files.sort()
print(files)

View 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)

View 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))

View File

@ -0,0 +1,5 @@
import glob
files = glob.glob("./")
print(files)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View 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.

View 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}")

View 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}")

View 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)

View 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)

View 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)