I am trying to write a python script to update the header (only the first line) of some huge files, but as the new header is not necessary to be the same size (in bytes) as the original one, is there anyway I could change the header without touching the rest of the huge file? or I have to read through them all and write them back to file?
In this article, we are going to add a header to a CSV file in Python. Method #1: Using header argument in to_csv () method. Initially, create a header in the form of a list, and then add that header to the CSV file using to_csv () method.
# Create a new variable called 'header' from the first row of the dataset header = df.iloc 0 first_name 1 last_name 2 age 3 preTestScore Name: 0, dtype: object # Replace the dataframe with a new one which does not contain the first row df = df[1:] # Rename the dataframe's column values with the header variable df.rename(columns = header)
The read_csv () method accepts the parameter header. You can pass header= [0, 1] to make the first two rows from the CSV file as a header of the dataframe. Using this way, you can create a dataframe with multiple header rows.
Initially, create a header in the form of a list, and then add that header to the CSV file using to_csv () method. The following CSV file gfg.csv is used for the operation: The CSV file gfg2.csv is created: Another approach of using DictWriter () can be used to append a header to the contents of a CSV file.
No, the only operations you can do on files without touching the whole file are truncation, replacement of same size, and appending.
You can, however, buffer relatively small parts of the file and write them after you've read all data currently residing in the new position, to avoid memory exhaustion. If speed is an issue, consider using mmap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With