Python之路 day2 按行读文件】的更多相关文章

#1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1: line = file.readline() if not line: break pass # do something #一行一行得从文件读数据,显然比较慢:不过很省内存. #在我的机器上读10M的sample.txt文件,每秒大约读32000行 #2. 用fileinput模块 # File: readline-examp…
读文件: 读取文件 f = open('\info.txt') fil = f.read() f.close() 按行读文件: f = open("info.txt") while 1: line = f.readline() line=line.strip('\n') # 去掉换行符 if not line: break print line f.close()…
1. 最基本的读文件方法: # File: readline-example-1.py   file = open("sample.txt")   while 1:     line = file.readline()     if not line:         break     pass # do something 一行一行得从文件读数据,显然比较慢:不过很省内存. 在我的机器上读10M的sample.txt文件,每秒大约读32000行 2. 用fileinput模块 #…
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #f,文件句柄;模式 a : append 追加文件内容 f = open("yesterday2",'a',encoding="utf-8") f.write("\nWhen i was yount i listen to the radio\n") f.write("I love Beijing Tiananm…
文件操作 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 Somehow, i…
Day2-转自金角大王 本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 1 names = ['Alex',"Tenglan",'Eric'] 通过下标访问列表中的元素,下标从0开始计数 1 >>> names[0] 2 'Alex' 3 >>> names[2] 4 'Eric' 5 >&g…
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa import sys print("sys default encoding: ",sys.getdefaultencoding()) #s 默认是 unicode 编码,Python默认是以Unicode编码的,本文件被设置成为utf-8编码 # 所以 s 没有 decode方法,所有编码之间的转换都是要先转成Unicode再进行encode成想要的编码格式 s = &…
-->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python来说,一切事物都是对象,对象是基于类创建的. 一.整型 #! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "Q1mi" """ int类的几个内建方法 """…
早就知道pygame模块,就是没怎么深入研究过,恰逢这周未没约到妹子,只能自己在家玩自己啦,一时兴起,花了几个小时写了个打飞机程序. 很有意思,跟大家分享下. 先看一下项目结构 """ PlayPlane/ |-- bin/ | |-- main.py 程序运行主体程序 |-- config/ | |-- settings.py 程序配置(例如: 游戏背景音乐的加载等) |-- material 程序素材放置(打飞机游戏素材放置) |-- ... |-- src/ 程序主体模…
1.列表练习name0 = 'wuchao'name1 = 'jinxin'name2 = 'xiaohu'name3 = 'sanpang'name4 = 'ligang' names = "wuchao jinxin xiaohu sanpang ligang"a=['wuchao','jinxin','xiaohu','sanpang','ligang'] #增删改查#增 切片print(a[1:])#取到最后print(a[1:-1])#取到倒数第二值print(a[1:-1:…