Actually, I have 1000 files but those 1000 files will have a format similar to the 3 csv files that I mentioned as Input Files, but their first row is quite different. If you see file 1, in first row there is ","comma, in file 2, the first row there is "sep=," and in file 3, it is totally fine(no weird characters). I am writing a python file where I want to clean the first row of data. So, in my example File 1 and File 2 should clean and File 3 should remain untouched should not require any cleaning. Just to note, every csv has different headers.
Input CSV files look like this(Actual -1000 files, but format of 1000 files will fall in any of these files categories)
File1.csv
,
Serial Num,Reading #,Date,Time,Label
640905,1,2018-04-55,7:45:58,
840806,2,1096-04-22,7:47:19,20
File2.csv
sep=,
SNum,Reading,Date,Time,La,,Check
640905,1,2028-04-55,18:45:58,10,,g
8840806,2,2096-04-22,20:47:19,20,,b
File3.csv
Date,Time,A_iop,b_m,c_om
12/10/2025,07:59:54,10,5559,89
21/10/2025,07:49:55,20,4545,90
My Python code is till now this:
from pathlib import Path
import pandas as pd
import time
import argparse
parser = argparse.ArgumentParser(description='Process')
parser.add_argument('path',help='define the directory to folder/file')
start = time.time()
def main(path_name):
if path_name.is_file():
fnames = [path_name]
else:
fnames = list(Path(path_name).glob("*.csv")) + list(Path(path_name).glob("*.xls"))
for fn in fnames:
if "csv" in str(fn):
df = pd.read_csv(str(fn))
#df = pd.read_csv(str(fn),index_col=False)
#df = pd.read_csv(str(fn),sep=',', index_col=False, dtype='unicode', header=1)
print(df)
if __name__== "__main__":
start = time.time()
args = parser.parse_args()
path = Path(args.path)
main(path)
print("Processed time: ", time.time()-start)
I want Output CSV look like this:
File1.csv(No Change)
Serial Num,Reading #,Date,Time,Label
640905,1,2018-04-55,7:45:58,
840806,2,1096-04-22,7:47:19,20
File2.csv(No Change)
SNum,Reading,Date,Time,La,,Check
640905,1,2028-04-55,18:45:58,10,,g
8840806,2,2096-04-22,20:47:19,20,,b
File3.csv(Same as Input)
Date,Time,A_iop,b_m,c_om
12/10/2025,07:59:54,10,5559,89
21/10/2025,07:49:55,20,4545,90
I am not able to formulate a logic in python how to clean those files before doing any further analysis? any help?