Skip to Content
Python数据处理
book

Python数据处理

by Jacqueline Kazil, Katharine Jarmul
July 2017
Intermediate to advanced
398 pages
11h 54m
Chinese
Posts & Telecom Press
Content preview from Python数据处理
156
8
用到了
writer
对象的
writerow
方法,将一个可迭代对象转换成一行逗号分隔的数据。本
行代码写入的是标题行。
利用列表生成式提取出所有回答(元组的第二个元素)。
将利用列表生成式创建的所有列表或回答写入
CSV
数据文件。
这里我们用到了学过的语法,也用到了一些新语法。我们已经学过如何用
with...as
将简
单函数的返回值赋值给一个变量名。这里我们希望将打开的文件赋值给
new_csv_file
量。这种语法通常用于文件和其他
I/O
对象,因为
Python
执行完
with
代码块中的代码之
后,它会自动关闭文件,这很棒!
此外,代码中我们用到了
CSV writer
对象,与
CSV reader
对象的用法类似。
writerow
可以
将包含所有数据列的列表写入到
CSV
文件中。
writerow
方法接受一个可迭代对象,所以一定要传入一个列表或元组。如果
你传入一个字符串,那么看到一些有趣的
CSV
(“
l,i,k,e, ,t,h,i,s
”)时不要惊讶。
我们还用到了列表生成式来创建标题列表和回答列表。由于我们不需要用这个函数生成一
个新对象或修改过的对象,所以没有返回任何值。这个函数可以帮我们复习目前学过的许
多概念。
如果你想用其他方法来保存数据,可以参考第
6
章给出的关于保存数据的建议。保存完清
洗过的数据之后,你可以继续进行后面的清洗过程,并对数据进行分析。
8.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

数据科学中的实用统计学(第2版)

数据科学中的实用统计学(第2版)

Peter Bruce, Andrew Bruce, Peter Gedeck
Java持续交付

Java持续交付

Daniel Bryant, Abraham Marín-Pérez
解密金融数据

解密金融数据

Justin Pauley

Publisher Resources

ISBN: 9787115459190