简体   繁体   中英

How to read CSV file in Python?

I'm using Spyder for Python 2.7 on Windows 8. I'm trying to open and read a csv file and see all the data stored in it, but this is what I get instead:

runfile('C:/Users/John/Documents/Python Scripts/FLInsuraneFile.py', wdir='C:/Users/John/Documents/Python Scripts')
<_io.TextIOWrapper name='FL_insurance_sample.csv' mode='r' encoding='cp1252'>

How can I open the file properly?

You can use builtin library

import csv
with open('names.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['first_name'], row['last_name'])

https://docs.python.org/3.5/library/csv.html

You can use the pandas library:

import pandas as pd
csvfile = pd.read_csv('path_to_file')
print(csvfile)

If you want to add custom headers to the file use the names argument otherwise it will just take the first row of the file as the header.

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

First things first, you must understand the inner-workings of a CSV file. CSV file are made up of rows and columns, like this:

| NAME  |  AGE |  ROOM |
| ---------------------|
| Kaleb |  15  |   256 |
| ---------------------|
| John  |  15  |   257 |
| ---------------------|
| Anna  |  16  |   269 |

Where the vertical elements are columns, and the horizontal elements are rows. Rows contain many types of data, like name/age/room. Columns contain only one type of data, like name.

Moving on, here is an example function to read the CSV. Please carefully study the code.

def read_csv(csv_file):
    data = []
    with open(csv_file, 'r') as f:

        # create a list of rows in the CSV file
        rows = f.readlines()

        # strip white-space and newlines
        rows = list(map(lambda x:x.strip(), rows))

        for row in rows:

            # further split each row into columns assuming delimiter is comma 
            row = row.split(',')

            # append to data-frame our new row-object with columns
            data.append(row)

    return data

Now why would I do that? Well, this function allows you to access your CSV file by row/column. Meaning it is easier to index. Look at this example using the above function:

csvFile = 'test.csv'

# invoke our function 
data = read_csv(csvFile)

# get row 1, column 2 of file
print(data[1][2])

# get entirety of row 2
print(data[2])

# get row 0, columns 1 & 2
print(data[0][1], data[0][2])

As you can see, we can easily access different parts of the file by using our read_csv() function and creating a nested-list object. Finally, if you want to print to the entire file, you simply use a for loop after creating the data-object.

data = read_csv(csvFile)

for row in data:
    print(row)

In conclusion, Pandas is great for big-data science, but if you just want to read/access the CSV, this function is just fine. No need to install big packages for little tasks, unless of course you want to :) .

Good luck!

You can use Table Base.

import tablebase

My_Table = tablebase.CsvTable("path/to/your.csv")
print(My_Table.table_content)

For full documentation of Table Base see python.centillionware.com/tablebase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM