第16课 Python循环体for遍历文件和元组
2017-01-12 15:22
543 查看
1. Python循环遍历元组(tuples)
Python的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
tup=(1,2,3,4,5)
for t in tup:
print t
else:
print 'out top'
============= RESTART: =============
1
2
3
4
5
out top
Python循环遍历文件
语法格式:
for val in files
例如for n in open(‘a.txt’,’r’).readlines()
其中a.txt代表的是xx文件,而r代表的是读取方式
>>> help(file.read)
Help on method_descriptor:
read(...)
read([size]) -> read at most size bytes, returned as a string.
If the size argument is negative or omitted, read until EOF is reached.
Notice that when in non-blocking mode, less data than what was requested
may be returned, even if no size parameter was given.
>>> help(file.readlines)
Help on method_descriptor:
readlines(...)
readlines([size]) -> list of strings, each a line from the file.
Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.
>>> help(file.readline)
Help on method_descriptor:
readline(...)
readline([size]) -> next line from the file, as a string.
Retain newline. A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.
Python的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
tup=(1,2,3,4,5)
for t in tup:
print t
else:
print 'out top'
============= RESTART: =============
1
2
3
4
5
out top
Python循环遍历文件
语法格式:
for val in files
例如for n in open(‘a.txt’,’r’).readlines()
其中a.txt代表的是xx文件,而r代表的是读取方式
>>> help(file.read)
Help on method_descriptor:
read(...)
read([size]) -> read at most size bytes, returned as a string.
If the size argument is negative or omitted, read until EOF is reached.
Notice that when in non-blocking mode, less data than what was requested
may be returned, even if no size parameter was given.
>>> help(file.readlines)
Help on method_descriptor:
readlines(...)
readlines([size]) -> list of strings, each a line from the file.
Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.
>>> help(file.readline)
Help on method_descriptor:
readline(...)
readline([size]) -> next line from the file, as a string.
Retain newline. A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.
相关文章推荐
- 第15课 Python中循环体for基础
- Python学习(三)——map和reduce函数
- 笨办法学Python-习题2:注释和井号
- python学习记录--set
- YATE界面生成模板引擎 《Head First Python》第七章
- 第13课 Python中while循环
- 第12课 Python中if分支语句表达式结构
- DRY Principles through Python Decorators
- 第11课 Python中if分支结构
- python学习记录--字典
- 图文详解 Python+Eclipse 环境搭建
- python (numpy基础) (pandas基础)(正则表达式)
- python从零开始写爬虫(2)
- 用pyinstaller打包一个exe程序
- python学习记录--元组
- 《Programming Collective Intelligence》笔记之 Chapter 2
- 比较全的python cmd模块的用法
- Python 列表、元组、字典t的常用方法
- 关于Pywin32模块安装找不到路径问题
- Python中time, strftime和strptime