Remove first line of CSV file

Mar 15, 2015 articles, code

Recently I needed to run  a script to remove the first line of a CSV file and was getting stumped! Finally after some testing and trouble shooting and a little Google help I found a solution. And I am hoping this post will find it’s way to others with similar needs. And credit to stackoverflow.

This solution is in PHP.

Firs you need to read the document, store the data in memory and then manipulate it and finally rewrite the document. Code is below. This code edits a file on your local machine and of course you could incorporate it to a server file, but it is strictly for editing a file and not used for any database work.

//Read File
$file = fopen('filewithfullpath.csv', 'r');
$data=array();
while (($data_tmp = fgetcsv($file, 1000, ",")) !== FALSE) {
   $data[] = $data_tmp;
}
fclose($file);

//Remove first line
array_shift($data);

//Write File
$file = fopen('filewithfullpath.csv', 'w');

//Set UT-8
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
foreach($data as $d) {
   fputcsv($file, $d, ',');
}
fclose($file);

Some trouble areas I had:

I also needed the final csv file to be in a UT-8 encoding, fputcsv function doesn’t do that for you so I found a nifty bit of code online that help achieve that (see Set UT-8 comment in code above) fprintf prints a encoding line before fputcsv writes the new data as csv.

The second thing I came across is I was pulling up the newly edited (remove first line from csv) document inside of notepad on a Windows machine and it wasn’t showing me any line breaks, and so I thought my code wasn’t working and turns out it was working I just needed to view it in a different editor, I pulled it up in my code editor and sure enough there were line breaks as it should of been. Something about notepad not reading CF breaks…something along those lines.

Python solution to remove first line of CSV file.

import csv

filename = "your_file.csv"

# Read the contents of the file
with open(filename, "r") as file:
    lines = file.readlines()

# Exclude the first line
lines = lines[1:]

# Write the modified contents back to the file
with open(filename, "w", newline="") as file:
    file.writelines(lines)

print("First line removed successfully.")

Bash / Shell solution to remove first line of CSV file.

#!/bin/bash

filename="your_file.csv"
tmpfile="temp.csv"

# Skip the first line of the CSV file and save the result to a temporary file
tail -n +2 "$filename" > "$tmpfile"

# Replace the original file with the temporary file
mv "$tmpfile" "$filename"

echo "First line removed successfully."