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数据处理
处理
Excel
文件
65
count = 0
for
i
in
range(1000):
if
count < 10:
print
i
count += 1
print
'Count: ', count
将变量
count
初始值设为
0
创建一个循环,循环范围是从
0
999
测试计数是否小于
10
,如果是的话,输出
1
增加
count
的值,这样计数会随着循环次数增加而增加。
输出最终计数。
在我们的代码中添加一个计数器,这样就可以逐项查看每一行和每一个单元格,找出我们
需要提取的内容。一定要注意计数器在代码中的位置——放在单元格的循环里或者放在行
的循环里,结果可能会大不相同。
修改
for
循环的代码如下:
count = 0
for
i
in
xrange(sheet.nrows):
if
count < 10:
row = sheet.row_values(i)
print
i, row
count += 1
输出行编号
i
和对应行的内容,这样我们可以看到每一行包含的信息。
现在回头看一下我们期望的最终输出格式,我们真正要搞清楚的是国家名字是从哪行开始
的。要记住,国家名字是最终输出字典的第一个键:
{
u'Afghanistan': {...},
u'Albania': {...},
...
}
上面脚本中含有计数器,并且控制语句是
count < 10
,运行后你会发现,输出中并没有包
含国家名字出现的那一行。
要跳过几行才能找到感兴趣的数据,所以我们要想办法确定从哪一行开始采集数据。在上 ...
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