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数据处理
供机器读取的数据
39
现在我们已经能够导入数据并对数据进行遍历,下面开始对数据进行真正的探索。
3.1.2
 将代码保存到文件中并在命令行中运行
作为开发者,在写代码时,即使是中间过程的部分代码片段,你也会希望保存下来,以便
后续检查和使用。保持代码结构清晰,并及时保存代码,即使中途被打断,你也可以从上
次中断的地方顺畅地继续工作。
我们将到目前为止所有的代码保存到文件中并运行。代码应该是这样的(如果你还没有完
成这一步的话,打开文本编辑器,创建一个新文件,将这段代码输入进去):
import
csv
csvfile = open('data-text.csv', 'rb')
reader = csv.reader(csvfile)
for
row
in
reader:
print
row
注意大小写、间距和换行。如果各行的代码间距各不相同或者大小写错误的
话,代码是无法正常运行的。一定要严格按上面的代码输入,利用四个空格
来缩进。这一点很重要,因为
Python
区分大小写,并利用缩进来表示代码的
结构。
用文本编辑器将代码保存为
.py
Python
)文件。完整的文件名应该是像这样的:
import_
csv_data.py
将数据文件
data-text.csv
放到你刚刚保存
Python
文件的同一个文件夹内。如果你想把文件
放到其他位置,需要对文件位置对应的代码做适当修改。
打开不同位置的文件
在目前的代码中,我们将文件路径传入
open
函数,像这样:
open('data-text.csv', 'rb') ...
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