如何在Python中读取CSV文件? [英] How to read CSV file in Python?

查看:578
本文介绍了如何在Python中读取CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows 8上使用Spyder for Python 2.7.我试图打开并读取一个csv文件,并查看其中存储的所有数据,但这是我得到的:

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'>

如何正确打开文件?

推荐答案

首先,您必须了解CSV文件的内部功能. CSV文件由行和列组成,如下所示:

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 |

其中垂直元素是列,水平元素是行.行包含许多类型的数据,例如名称/年龄/房间.列仅包含一种类型的数据,例如name.

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.

继续,这是读取CSV的示例功能. 请仔细研究代码.

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

现在我为什么要这样做?好的,此功能使您可以按行/列访问CSV文件.意味着更容易建立索引.使用上面的函数查看此示例:

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])

如您所见,通过使用我们的read_csv()函数并创建一个嵌套列表对象,我们可以轻松访问文件的不同部分.最后,如果要打印到整个文件,只需在创建数据对象后使用for循环.

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)

最后,Pandas非常适合大数据科学,但如果您只是 要读取/访问CSV,此功能就可以了.除非您要:),否则无需为小任务安装大软件包.

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 :) .

祝你好运!

这篇关于如何在Python中读取CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆