Updated lecture code 11/4/2025

This commit is contained in:
Edward Bigos 2025-11-04 10:45:17 -05:00
parent eed5c997b5
commit 0963374b0a
9 changed files with 1918 additions and 4 deletions

View file

@ -1,9 +1,11 @@
import sys
line = "7/5/2016,123,638.5"
# split line on commas
lineFields = line.split(',')
print(line)
print(lineFields)

View file

@ -0,0 +1,7 @@
Now the file has more content!Now the file has more content!Now the file has more content!
Now the file has more content!
Now the file has more content!
Now the file has more content!
Now the file has more content!
Now the file has more content!
Now the file has more content!

View file

@ -1,7 +1,7 @@
# https://www.w3schools.com/python/python_file_write.asp
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.write("Now the file has more content!\n")
f.close()
#open and read the file after the appending:

View file

@ -0,0 +1,24 @@
# Read a csv file from the current directory
# import the csv function library
import csv
filename = "data.csv"
with open(filename,'r') as csvfile:
csvData = csv.reader(csvfile,delimiter = ',',quotechar = '"')
AvgDuration = 0
AvgPulse = 0
rowCounter = 0
for row in csvData:
if rowCounter != 0:
AvgDuration = (AvgDuration + float(row[0]) ) /2
AvgPulse = (AvgPulse + float(row[1]) )/2
# print(row, AvgDuration, AvgPulse)
rowCounter = rowCounter + 1
print(f"Average Duration = {AvgDuration:.1f}")
print(f"Average Pulse = {AvgPulse:.0f}")

View file

@ -4,7 +4,7 @@
# for more information
# Just try reading it line by line on the first attempt.
fileName = "testdata.txt"
fileName = "testxdata.txt"
try:
inFile = open(fileName,'r')
@ -18,8 +18,8 @@ try:
line = line.rstrip("\n")
count = count + 1
except:
print("Some error occurred. Ending here")
except Exception as e:
print("Some error occurred. Ending here",e)
exit(-1)
inFile.close()

View file

@ -0,0 +1,7 @@
# https://www.w3schools.com/python/pandas/pandas_csv.asp
import pandas as pd
df = pd.read_csv('powerball.csv')
print(df.to_string())

View file

@ -0,0 +1,9 @@
# https://www.w3schools.com/python/pandas/pandas_csv.asp
import pandas as pd
df = pd.read_csv('powerball.csv')
print(df.head())
print(df.tail())

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,5 @@
# https://www.w3schools.io/file/csv-extension-introduction/
# Visualize Python https://pythontutor.com/visualize.html#mode=edit