Python csv reader last row

Python CSV tutorial — read write CSV

Python CSV tutorial shows how to read and write CSV data with Python csv module.

CSV

is a very popular import and export data format used in spreadsheets and databases. Each line in a CSV file is a data record. Each record consists of one or more fields, separated by commas. While CSV is a very simple data format, there can be many differences, such as different delimiters, new lines, or quoting characters.

Python csv module

The csv module implements classes to read and write tabular data in CSV format. The csv module’s reader and writer objects read and write sequences. Programmers can also read and write data in dictionary form using the DictReader and DictWriter classes.

Читайте также:  Css для кнопки пример

Python CSV methods

Using Python csv module

To use Python CSV module, we import csv .

Python CSV reader

The csv.reader method returns a reader object which iterates over lines in the given CSV file.

$ cat numbers.csv 16,6,4,12,81,6,71,6

The numbers.csv file contains numbers.

#!/usr/bin/python import csv f = open('numbers.csv', 'r') with f: reader = csv.reader(f) for row in reader: for e in row: print(e)

In the code example, we open the numbers.csv for reading and read its contents.

for row in reader: for e in row: print(e)

With two for loops, we iterate over the data.

$ ./read_csv.py 16 6 4 12 81 6 71 6

Python CSV reader with different delimiter

The csv.reader method allows to use a different delimiter with its delimiter attribute.

$ cat items.csv pen|cup|bottle chair|book|tablet

The items.csv contains values separated with ‘|’ character.

#!/usr/bin/python import csv f = open('items.csv', 'r') with f: reader = csv.reader(f, delimiter="|") for row in reader: for e in row: print(e)

The code example reads and displays data from a CSV file that uses a ‘|’ delimiter.

$ ./read_csv2.py pen cup bottle chair book tablet

Python CSV DictReader

The csv.DictReader class operates like a regular reader but maps the information read into a dictionary. The keys for the dictionary can be passed in with the fieldnames parameter or inferred from the first row of the CSV file.

$ cat values.csv min,avg,max 1, 5.5, 10 2, 3.5, 5

The first line of the file consists of dictionary keys.

#!/usr/bin/python # read_csv3.py import csv f = open('values.csv', 'r') with f: reader = csv.DictReader(f) for row in reader: print(row['min'], row['avg'], row['max'])

The example reads the values from the values.csv file using the csv.DictReader .

for row in reader: print(row['min'], row['avg'], row['max'] )

The row is a Python dictionary and we reference the data with the keys.

Python CSV writer

The csv.writer method returns a writer object which converts the user’s data into delimited strings on the given file-like object.

#!/usr/bin/python import csv nms = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]] f = open('numbers2.csv', 'w') with f: writer = csv.writer(f) for row in nms: writer.writerow(row)

The script writes numbers into the numbers2.csv file. The writerow method writes a row of data into the specified file.

$ cat numbers2.csv 1,2,3,4,5,6 7,8,9,10,11,12

It is possible to write all data in one shot. The writerows method writes all given rows to the CSV file.

#!/usr/bin/python import csv nms = [[1, 2, 3], [7, 8, 9], [10, 11, 12]] f = open('numbers3.csv', 'w') with f: writer = csv.writer(f) writer.writerows(nms)

The code example writes three rows of numbers into the file using the writerows method.

Python CSV DictWriter

The csv.DictWriter class operates like a regular writer but maps Python dictionaries into CSV rows. The fieldnames parameter is a sequence of keys that identify the order in which values in the dictionary passed to the writerow method are written to the CSV file.

#!/usr/bin/python import csv f = open('names.csv', 'w') with f: fnames = ['first_name', 'last_name'] writer = csv.DictWriter(f, fieldnames=fnames) writer.writeheader() writer.writerow() writer.writerow() writer.writerow()

The example writes the values from Python dictionaries into the CSV file using the csv.DictWriter .

writer = csv.DictWriter(f, fieldnames=fnames)

New csv.DictWriter is created. The header names are passed to the fieldnames parameter.

The writeheader method writes the headers to the CSV file.

The Python dictionary is written to a row in a CSV file.

$ cat names.csv first_name,last_name John,Smith Robert,Brown Julia,Griffin

Python CSV custom dialect

A custom dialect is created with the csv.register_dialect method.

#!/usr/bin/python import csv csv.register_dialect("hashes", delimiter="#") f = open('items3.csv', 'w') with f: writer = csv.writer(f, dialect="hashes") writer.writerow(("pens", 4)) writer.writerow(("plates", 2)) writer.writerow(("bottles", 4)) writer.writerow(("cups", 1))

The program uses a (#) character as a delimiter. The dialect is specified with the dialect option in the csv.writer method.

$ cat items3.csv pens#4 plates#2 bottles#4 cups#1

In this tutorial we have worked with CSV in Python.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

Find the Last Row from a CSV Input Python

Reading the last row or the row having the latest value in csv file

The csv reader is a generator, you can convert that to a list. You can get the last data in the last line with the following code sample:

import csv

with open('test.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=' ')
print(list(csv_reader)[-1][-1])

Get last row in last column from a csv file using python

You can do this: if you want the very last row

with open('data.csv', 'r') as csv: 
data = [[x.strip() for x in line.strip().split(',')] for line in csv.readlines()][-1][-1]
print(data)

or if you want all the last elements in each row

with open('data.csv', 'r') as csv: 
data = [line.strip().split(',')[-1] for line in csv.readlines()]
print(data)

Python csv read and write, (write only saves last row)

write function overwrites, when you open without «a» option. Do something like this,

f = io.open(filename, "a", encoding="utf-8") # not "w"

How to get the first value of last row of a CSV file using python

if you want to use python only :

with open('csv_file.csv','r') as f: 
opened_file = f.readlines()
var = opened_file[-1].split(',')[0]

Get Values from last Row of .csv File

I think you’re on a Unix-y system so something simple like this should work + it’s fast because it doesn’t read the whole file:

import subprocess
last_line = subprocess.check_output(["tail", "-1", "DataLogging.csv"])
# to get the values (parsing via the csv module seems excessive for one line)
temp_hum = [float(x) for x in last_line.split()[2:]]

A cross-platform solution using seek() could be made by implemeting tail s approach. A comment in tail ‘s source code explains how it works:

Print the last N_LINES lines from the end of file FD. Go backward
through the file, reading ‘BUFSIZ’ bytes at a time (except probably
the first), until we hit the start of the file or have read NUMBER
newlines. START_POS is the starting position of the read pointer
for the file associated with FD (may be nonzero). END_POS is the
file offset of EOF (one larger than offset of last byte). Return
true if successful.

How to access the second to last row of a csv file using python Pandas?

Get last row of CSV file and append it onto the end of the same CSV file

Answering my own question:

I have figured out how to maintain the order of columns and append to the end of the existing dataframe. It is worth noting that pd.tail() does not maintain the order of columns.

df = pd.read_csv('file.csv')

df_fluxes = df.iloc[[-1] * n]

df_fluxes.to_csv(run_dir+'em_line_fluxes.csv', mode='a', header=False, index=False)

Источник

Оцените статью