How to read a CSV file in Python?

11

I need to read a very large CSV file (+ 300K lines).

What is the best way to read a CSV file in python ?

    
asked by anonymous 09.11.2015 / 23:31

4 answers

12

The simplest way to read the file:

import csv
ficheiro = open('ficheiro_csv.csv', 'rb')
reader = csv.reader(ficheiro)
for linha in reader:
    print linha

However it is a good practice to open the file as follows:

import csv
with open('ficheiro_csv.csv', 'rb') as ficheiro:
    reader = csv.reader(ficheiro)
    for linha in reader:
        print linha

In case the file has an alternative format we must declare the delimiter and whether or not it has pelicas:

import csv
with open('ficheiro', 'rb') as ficheiro:
    reader = csv.reader(ficheiro, delimiter=':', quoting=csv.QUOTE_NONE)
    for linha in reader:
        print linha

A more developed form already able to deal with possible errors:

import csv, sys
nome_ficheiro = 'ficheiro.csv'
with open(nome_ficheiro, 'rb') as ficheiro:
    reader = csv.reader(ficheiro)
    try:
        for linha in reader:
            print linha
    except csv.Error as e:
        sys.exit('ficheiro %s, linha %d: %s' % (nome_ficheiro, reader.line_num, e))

The csv.reader () methods only yield one line at a time, so you can handle large files.

Some examples can be seen in detail here .

    
10.11.2015 / 11:57
4

If your csv file is too large, you can use the read_csv method of Lib Pandas. At first it performs better than the standard Python csv.

import pandas as pd
print pd.read_csv('file.csv')

Return is an object of type DataFrame.

    
12.11.2015 / 16:46
3

See below as:

>>> import csv
>>> with open('eggs.csv', 'rb') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print ', '.join(row)
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

Import the csv module, open the "eggs.csv" file for reading, iterate over each line and print, or do whatever you want with the file.

This method is valid for python 2.7 onwards.

    
09.11.2015 / 23:34
-1
import csv
import numpy as np


arquivo = open('pessoas.csv')
nome,idade,email=np.loadtxt('pessoas.csv',
                            delimiter=',',
                            unpack=True,
                            dtype='str')


linhas = csv.reader(arquivo)

for linha in linhas:
    print(linha)
i=1

while i < len(nome):
    print(nome[i])
    i=i+1
    
19.12.2018 / 19:10