I'm trying to use the following function to display data in two CSV files and be able to merge the files later but when I run it I only get the CSV headers with no actual content
import pandas as pd
# to store the data to text file 1
def RecordData1():
file1 = "dataSensor1.txt"
response = ""
outfile = open(file1, "w")
outfile.write("Sensor ID,City,PressureReading")
while response != '0':
SensorID, City, PressureReading = input("Enter sensor ID, city and its pressure reading:").split()
outfile.write("\n")
outfile.write(SensorID + ",")
outfile.write(City + ",")
outfile.write(PressureReading + ",")
response = input("Press enter to continue and zero(0) to exit:")
outfile.close()
# to store the data into text file 2
def RecordData2() :
file2 = "dataSensor2.txt"
response = ""
outfile1 = open(file2, "w")
outfile1.write("Sensor ID,City,PressureReading")
while response != '0':
SensorID, City, PressureReading = input("Enter sensor ID, city and its pressure reading:").split()
outfile1.write("\n")
outfile1.write(SensorID + ",")
outfile1.write(City + ",")
outfile1.write(PressureReading + ",")
response = input("Press enter to continue and zero(0) to exit:")
outfile1.close()
# to read and then combine the data from 2 text files by using pandas
def RetrieveRecord():
df1 = pd.read_csv('dataSensor1.txt', sep=",")
df2 = pd.read_csv('dataSensor2.txt', sep=",")
print(df1.to_string())
print(df2.to_string())
concatrecord(df1, df2) # function call
def concatrecord(df1, df2):
df3 = pd.concat([df1, df2], ignore_index=True, axis=0)
print(df3)
print(df3['Pressure reading'].max())
print(df3['Pressure reading'].min())
print('Average pressure reading',(max(df3)+min(df3)/2))
Filteritem(df3)
def Filteritem(df3):
choice = input('Please enter Sensor ID to be searched')
filterItem = df3[df3['Sensor ID'] == choice]
print(filterItem)
RecordData1()
RecordData2()
RetrieveRecord()
It's supposed to have two tables in two separate text files and be able to merge them later on into one file
Still not very sure what's the issue
Help would be highly appreciated,
Thank you