I have a .csv
file with headers.
I am trying to delete the header row and then open the same file for reading.
But the first line read is still the header line. How to I delete the header line and start reading from the first line of data?
Code snippet -
# Sort the cleaned file on r2
df = pd.read_csv(cleaned_file + ".csv", names=['r2','r5','r7','r12','r15','r70','r83'])
sorted_df = df.sort_values(by=["r2"], ascending=True)
sorted_df.to_csv(cleaned_file_sorted_on_ts + '.csv', index=False)
# Remove the header line from the cleaned_file_sorted_on_ts file
cmd = "tail -n +2 " + cleaned_file_sorted_on_ts + ".csv" + " > tmp.csv && mv tmp.csv " + cleaned_file_sorted_on_ts + ".csv"
print(cmd)
proc = Popen(cmd, shell=True, stdout=PIPE)
with open(cleaned_file_sorted_on_ts + ".csv","r") as infile:
first_line = infile.readline().strip('\n')
print("First line in cleaned file = {}".format(first_line))
Output I am getting is -
tail -n +2 /ghostcache/Run.multi.rollout/h2_lines_cleaned_sorted.csv > tmp.csv && mv tmp.csv /ghostcache/Run.multi.rollout/h2_lines_cleaned_sorted.csv
First line in cleaned file = r2,r5,r7,r12,r15,r70,r83
Traceback (most recent call last):
File "process_r83.py", line 51, in <module>
first_ts = int(float(first_line.split(',')[0]))
ValueError: could not convert string to float: 'r2'