Last active
July 19, 2023 04:29
-
-
Save gravityfargo/97101e44f6a860d91cbf90f3f1970136 to your computer and use it in GitHub Desktop.
ECE498 Preformat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import required modules | |
import os | |
# Set the header of the data file | |
# Date;Time;Global_active_power;Global_reactive_power;Voltage;Global_intensity;Sub_metering_1;Sub_metering_2;Sub_metering_3 | |
def main(): | |
# Open the data file and read its contents | |
contents = openFile('household_power_consumption.txt') | |
# Create a new file to store the processed data | |
newdata = open("data.csv", "w") | |
# Loop over the rows of the data | |
for row in contents: | |
# Split the time field into hours, minutes, and seconds | |
timelist = row[1].split(':') | |
# Check if the global intensity field is not empty and if the minute field is "00" and if global active power is not '?' | |
if len(row[5]) != 0 and timelist[1] == "00" and row[2] != "?": | |
# Split the date field into day, month, and year | |
datelist = row[0].split('/') | |
# Construct a new string with the date, global active power, global reactive power, voltage, and global intensity | |
# Formatting with date | |
newstr = datelist[2] + datelist[1] + datelist[0] + ' ' + row[2] + ' ' + row[3] + ' ' + row[4] + ' ' + row[5] | |
# Formatting without date | |
# newstr = row[2] + ' ' + row[3] + ' ' + row[4] + ' ' + row[5] | |
# Write the new string to the new data file | |
newdata.write(newstr) | |
newdata.write('\n') | |
# Close the new data file | |
newdata.close() | |
# This function returns a list of files in the current directory | |
def listFiles(): | |
itemsindir = [] | |
for path in os.listdir('.'): | |
itemsindir.append(path) | |
return itemsindir | |
# This function opens a file and reads its contents into a list | |
def openFile(filename): | |
contentsList = [] | |
lineitemsList = [] | |
try: | |
file = open(filename, 'r') | |
while True: | |
line = file.readline() | |
if not line: | |
break | |
lineitemsList = line.split(';') | |
contentsList.append(lineitemsList) | |
lineitemsList.clear | |
return contentsList | |
except: | |
print("Invalid File.") | |
exit() | |
# Execute the main function | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment