使用 Python 读取电子表格中的数据
CSV
![](/data/attachment/album/202004/12/093539jz81k1wgeh85akoe.jpg)


[Python](https://python.org) 是最流行、功能最强大的编程语言之一。由于它是自由开源的，因此每个人都可以使用。大多数 Fedora 系统都已安装了该语言。Python 可用于多种任务，其中包括处理逗号分隔值（CSV）数据。CSV文件一开始往往是以表格或电子表格的形式出现。本文介绍了如何在 Python 3 中处理 CSV 数据。


CSV 数据正如其名。CSV 文件按行放置数据，数值之间用逗号分隔。每行由相同的*字段*定义。简短的 CSV 文件通常易于阅读和理解。但是较长的数据文件或具有更多字段的数据文件可能很难用肉眼解析，因此在这种情况下计算机做得更好。


这是一个简单的示例，其中的字段是 `Name`、`Email` 和 `Country`。在此例中，CSV 数据将字段定义作为第一行，尽管并非总是如此。



```
Name,Email,Country
John Q. Smith,jqsmith@example.com,USA
Petr Novak,pnovak@example.com,CZ
Bernard Jones,bjones@example.com,UK
```

### 从电子表格读取 CSV


Python 包含了一个 `csv` 模块，它可读取和写入 CSV 数据。大多数电子表格应用，无论是原生（例如 Excel 或 Numbers）还是基于 Web 的（例如 Google Sheet），都可以导出 CSV 数据。实际上，许多其他可发布表格报告的服务也可以导出为 CSV（例如，PayPal）。


Python `csv` 模块有一个名为 `DictReader` 的内置读取器方法，它可以将每个数据行作为有序字典 （`OrderedDict`） 处理。它需要一个文件对象访问 CSV 数据。因此，如果上面的文件在当前目录中为 `example.csv`，那么以下代码段是获取此数据的一种方法：



```
f = open('example.csv', 'r')
from csv import DictReader
d = DictReader(f)
data = []
for row in d:
    data.append(row)
```

现在，内存中的 `data` 对象是 `OrderedDict` 对象的列表：



```
[OrderedDict([('Name', 'John Q. Smith'),
               ('Email', 'jqsmith@example.com'),
               ('Country', 'USA')]),
  OrderedDict([('Name', 'Petr Novak'),
               ('Email', 'pnovak@example.com'),
               ('Country', 'CZ')]),
  OrderedDict([('Name', 'Bernard Jones'),
               ('Email', 'bjones@example.com'),
               ('Country', 'UK')])]
```

引用这些对象很容易：



```
>>> print(data[0]['Country'])
USA
>>> print(data[2]['Email'])
bjones@example.com
```

顺便说一句，如果你需要处理没有字段名标题行的 CSV 文件，那么 `DictReader` 类可以让你定义它们。在上面的示例中，添加 `fieldnames` 参数并传递一系列名称：



```
d = DictReader(f, fieldnames=['Name', 'Email', 'Country'])
```

### 真实例子


我最近想从一长串人员名单中随机选择一个中奖者。我从电子表格中提取的 CSV 数据是一个简单的名字和邮件地址列表。


幸运的是，Python 有一个有用的 `random` 模块，可以很好地生成随机值。该模块 `Random` 类中的 `randrange` 函数正是我需要的。你可以给它一个常规的数字范围（例如整数），以及它们之间的步长值。然后，该函数会生成一个随机结果，这意味着我可以在数据的总行数范围内获得一个随机整数（或者说是行号）。


这个小程序运行良好：



```
from csv import DictReader
from random import Random

d = DictReader(open('mydata.csv'))
data = []
for row in d:
    data.append(row)

r = Random()
winner = data[r.randrange(0, len(data), 1)]
print('The winner is:', winner['Name'])
print('Email address:', winner['Email'])
```

显然，这个例子非常简单。电子表格本身包含了复杂的分析数据的方法。但是，如果你想在电子表格应用之外做某事，Python 或许是一种技巧！


题图由 [Isaac Smith](https://unsplash.com/@isaacmsmith?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) 拍摄，发表于 [U​​nsplash](https://unsplash.com/s/photos/spreadsheets?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)。




---


via: <https://fedoramagazine.org/using-data-from-spreadsheets-in-fedora-with-python/>


作者：[Paul W. Frields](https://fedoramagazine.org/author/pfrields/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
[Python](https://python.org) is one of the most popular and powerful programming languages available. Because it’s free and open source, it’s available to everyone — and most Fedora systems come with the language already installed. Python is useful for a wide variety of tasks, but among them is processing comma-separated value (**CSV**) data. CSV files often start off life as tables or spreadsheets. This article shows how to get started working with CSV data in Python 3.

CSV data is precisely what it sounds like. A CSV file includes one row of data at a time, with data values separated by commas. Each row is defined by the same *fields*. Short CSV files are often easily read and understood. But longer data files, or those with more fields, may be harder to parse with the naked eye, so computers work better in those cases.

Here’s a simple example where the fields are *Name*, *Email*, and *Country*. In this example, the CSV data includes a field definition as the first row, although that is not always the case.

Name,Email,Country John Q. Smith,jqsmith@example.com,USA Petr Novak,pnovak@example.com,CZ Bernard Jones,bjones@example.com,UK

## Reading CSV from spreadsheets

Python helpfully includes a *csv* module that has functions for reading and writing CSV data. Most spreadsheet applications, both native like Excel or Numbers, and web-based such as Google Sheets, can export CSV data. In fact, many other services that can publish tabular reports will also export as CSV (PayPal for instance).

The Python *csv* module has a built in reader method called *DictReader* that can deal with each data row as an ordered dictionary (OrderedDict). It expects a file object to access the CSV data. So if our file above is called *example.csv* in the current directory, this code snippet is one way to get at this data:

f = open('example.csv', 'r') from csv import DictReader d = DictReader(f) data = [] for row in d: data.append(row)

Now the *data* object in memory is a list of OrderedDict objects :

[OrderedDict([('Name', 'John Q. Smith'), ('Email', 'jqsmith@example.com'), ('Country', 'USA')]), OrderedDict([('Name', 'Petr Novak'), ('Email', 'pnovak@example.com'), ('Country', 'CZ')]), OrderedDict([('Name', 'Bernard Jones'), ('Email', 'bjones@example.com'), ('Country', 'UK')])]

Referencing each of these objects is easy:

>>> print(data[0]['Country']) USA >>> print(data[2]['Email']) bjones@example.com

By the way, if you have to deal with a CSV file with no header row of field names, the *DictReader* class lets you define them. In the example above, add the *fieldnames* argument and pass a sequence of the names:

d = DictReader(f, fieldnames=['Name', 'Email', 'Country'])

## A real world example

I recently wanted to pick a random winner from a long list of individuals. The CSV data I pulled from spreadsheets was a simple list of names and email addresses.

Fortunately, Python also has a helpful *random* module good for generating random values. The *randrange* function in the *Random* class from that module was just what I needed. You can give it a regular range of numbers — like integers — and a step value between them. The function then generates a random result, meaning I could get a random integer (or row number!) back within the total number of rows in my data.

So this small program worked well:

from csv import DictReader from random import Random d = DictReader(open('mydata.csv')) data = [] for row in d: data.append(row) r = Random() winner = data[r.randrange(0, len(data), 1)] print('The winner is:', winner['Name']) print('Email address:', winner['Email'])

Obviously this example is extremely simple. Spreadsheets themselves include sophisticated ways to analyze data. However, if you want to do something outside the realm of your spreadsheet app, Python may be just the trick!

*Photo by Isaac Smith on Unsplash.*

## Christian

Thank you!. It is an excellent example.

## Paweł

How about random.choice(data) ? 🙂

## Paul W. Frields

There are almost always multiple ways to solve a problem.

## Mark

Very useful for people comfortable with using Python I suppose. However most people would not use Python for simple (or even complex) scripting.

The ‘real world example can be simply done in bash (or any other shell) without needing to import functions or use arrays.

A good article in the way it explains the layout of the array created and how to reference the results, as a non Python user I was easily able to see how it all works with that information.

Having seen this article I did a quick search on DictReader and found this page https://courses.cs.washington.edu/courses/cse140/13wi/csv-parsing.html where the example shows using Dictreader against a CSV file in a way that almost emulates the intent of a sql query against a database which does make the function seem a little useful for people who prefer working with large CSV files for data queries rather than databases.

I see there is also a DictWriter function, so pherhaps the article examples could be updated to show how to use the two functions in a SQL type way to convert a CSV file A into a CSV file B with fewer output fields/columns and fewer records based on a selection criteria ?. That may be a better example as the only main reasons I can think of for such python functions are to split out data in this way to create new smaller partial CSV datasets, perhaps in a corporate environment which is too cheap to use a database and stores data in spreadsheets but only wants people to see subsets of the spreadsheet.

## Paul W. Frields

Hopefully the formatting above is fixed for you now. Thank you for the contribution!

## Vernon Van Steenkist

While I agree 100% with your approach and your observation that using simple tools that process files line by line are much more efficient and scalable to large data sets than loading everything into Python tables, I would like to suggest a couple of improvements:

First

linecount=

I prefer

linecount=$(wc -l mydata.csv | cut -f 1 -d” “)

because it only processes every line in mydata.csv once.

Second

head -${linerandom} mydata.csv | tail -1

I believe can be more simply written

sed -n $linerandom’p’ mydata.csv

## Jeffrey P Burdick

I am surprised we’re talking about tabulated data in Python and not talking about Pandas, which is a WONDERFUL library for working with tabulated data.

https://pandas.pydata.org/