Skip to Content
Python数据分析基础
book

Python数据分析基础

by Clinton W. Brownley
August 2017
Intermediate to advanced
274 pages
7h 44m
Chinese
Posts & Telecom Press
Content preview from Python数据分析基础
58
2
假设输入文件和
Python
脚本都保存在你的桌面上,你也没有在命令行或终端行窗口中改变
目录,在命令行中输入以下命令,然后按回车键运行脚本(如果你使用
Mac
,需要对新的
脚本先运行
chmod
命令,使它成为可执行的):
python 2csv_reader_parsing_and_write.py supplier_data.csv\
output_files\2output.csv
你可以看到输出被打印到屏幕上,如图
2-9
所示。
2-9:运行 Python 脚本得到的输出
输入文件中的所有行都被打印到了屏幕上,同时被写入到输出文件。你可以看到,
Python
内置的
csv
模块处理了嵌入数据的逗号问题,正确地将每一行拆分成了
5
个值。
我们知道了如何使用
csv
模块来读取、处理和写入
CSV
文件,下面开始学习如何筛选出特
定的行以及如何选择特定的列,以便可以有效地抽取出需要的数据。
2.2
 筛选特定的行
有些时候,你并不需要文件中所有的数据。例如,你可能只需要一个包含特定词或数字的
行的子集,或者是与某个具体日期关联的行的子集。在这些情况下,可以用
Python
筛选出
特定的行来使用。
你应该很熟悉如何在
Excel
中手动筛选行,但是本章的重点在于提高你的能力,使你既能
处理因为体积太大以致
Excel
不能打开的
CSV
文件,又能处理多个
CSV
文件。因为要通
过手动处理这些文件,时间花费太多了。
下面各小节演示了在输入文件中筛选出特定行的
3
种方法:
行中的值满足某个条件
行中的值属于某个集合
行中的值匹配于某个模式(正则表达式) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据驱动力:企业数据分析实战

数据驱动力:企业数据分析实战

Carl Anderson
大数据项目管理:从规划到实现

大数据项目管理:从规划到实现

Ted Malaska, Jonathan Seidman

Publisher Resources

ISBN: 9787115463357