Updated lecture code 11/4/2025
This commit is contained in:
parent
eed5c997b5
commit
0963374b0a
9 changed files with 1918 additions and 4 deletions
|
|
@ -1,9 +1,11 @@
|
|||
import sys
|
||||
|
||||
line = "7/5/2016,123,638.5"
|
||||
|
||||
# split line on commas
|
||||
lineFields = line.split(',')
|
||||
|
||||
print(line)
|
||||
print(lineFields)
|
||||
|
||||
|
||||
|
|
|
|||
7
bigos/lecture20251104/csvfiles/demofile2.txt
Normal file
7
bigos/lecture20251104/csvfiles/demofile2.txt
Normal 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!
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
24
bigos/lecture20251104/csvfiles/hack.py
Normal file
24
bigos/lecture20251104/csvfiles/hack.py
Normal 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}")
|
||||
|
||||
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
7
bigos/lecture20251104/powerball/pandasreadcsv01.py
Normal file
7
bigos/lecture20251104/powerball/pandasreadcsv01.py
Normal 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())
|
||||
9
bigos/lecture20251104/powerball/pandasreadcsv02.py
Normal file
9
bigos/lecture20251104/powerball/pandasreadcsv02.py
Normal 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())
|
||||
1864
bigos/lecture20251104/powerball/powerball.csv
Normal file
1864
bigos/lecture20251104/powerball/powerball.csv
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,4 +1,5 @@
|
|||
# https://www.w3schools.io/file/csv-extension-introduction/
|
||||
|
||||
# Visualize Python https://pythontutor.com/visualize.html#mode=edit
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue