Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

efficient way to change the header of a file in Python

Tags:

python

file

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?

like image 710
John Avatar asked Jun 30 '11 16:06

John


People also ask

How to add a header to a CSV file in Python?

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.

How do I change the header of a Dataframe?

# 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)

How to create a Dataframe with multiple header rows in Python?

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.

How to append a header to the contents of a CSV?

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.


1 Answers

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.

like image 174
phihag Avatar answered Sep 22 '22 03:09

phihag