Reputation: 75
I want to know the best way to reverse the lines of a big csv file (+50000 lines) in python 2.7 and rewrite it, avoiding the first line.
input:
A;B;C
1;2;3
4;5;6
output
A;B;C
4;5;6
1;2;3
I need to know how to do it in a efficient way in python 2.7.
Thank you guys,
menchopez
Upvotes: 4
Views: 7336
Reputation: 140276
read the csv file using csv
module and open the output also using csv
module. Now you're working with list
s as rows.
Use next
to write the title line as-is. Now that the first line is consumed, convert the rest of the data into a list
to read it fully and apply writerows
on the reversed list:
import csv
with open("in.csv") as fr, open("out.csv","wb") as fw:
cr = csv.reader(fr,delimiter=";")
cw = csv.writer(fw,delimiter=";")
cw.writerow(next(cr)) # write title as-is
cw.writerows(reversed(list(cr)))
writerows
is the fastest way of doing it, because it involves no python loops.
Python 3 users have to open the output file using open("out.csv","w",newline="")
instead.
Upvotes: 4
Reputation: 1856
Read as follows:
rows = []
first = True
for row in reader:
if first:
first = False
first_row = row
continue
rows.append(row)
write as follows:
rows.append(first_row)
writer.writerows(rows[::-1])
Upvotes: 1
Reputation: 1592
If you can use external libraries, the pandas library is good for large files:
import pandas as pd
# load the csv and user row 0 as headers
df = pd.read_csv("filepath.csv", header = 0)
# reverse the data
df.iloc[::-1]
If you cannot use external libraries:
import csv
with open("filepath.csv") as csvFile:
reader = csv.reader(csvFile)
# get data
data = [row for row in reader]
# get headers and remove from data
headers = data.pop(0)
# reverse the data
data_reversed = data[::-1]
# append the reversed data to the list of headers
output_data = headers.append(data_reversed)
Upvotes: 3