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数据分析基础
140
4
4-14:使用 MySQL 命令行客户端在 Suppliers 表中进行数据查询的结果
现在我们已经有了一个充满数据的数据表,下面就开始学习如何在数据库中进行查询,并
使用
Python
将查询结果写入
CSV
文件,而不是打印在屏幕上。
4.2.2
 查询一个表并将输出写入
CSV
文件
数据表中有了数据之后,最常见的下一个步骤就是使用查询从表中取出一组数据,用来进
行分析或满足某种商业需求。例如,你可能想知道哪些客户提供了最多的利润,或者哪些
费用超过了具体的阈值。
下面创建一个新的
Python
脚本。这个脚本会从
Suppliers
数据表中查询出一组特定记录,
然后将输出写入
CSV
输出文件。在这个例子中,我们想找出
Cost
列中的值大于
1000.00
的所有记录,并将这些记录所有列中的值输出。首先,在文本编辑器中输入下列代码,然
后将文件保存为
5db_mysql_write_to_file.py
1 #!/usr/bin/env python3
2
import
csv
3
import
MySQLdb
4
import
sys
5 # CSV输出文件的路径和文件名
6 output_file = sys.argv[1]
7 # 连接MySQL数据库
8 con = MySQLdb.connect(host='localhost', port=3306, db='my_suppliers', \
9 user='root', passwd='my_password') ...
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