python在循环中读取不同文件(python循环读取文件内容)
求python 循环读取多文件多行的代码
对python不大熟?
用的C语言的思路做的
文件随便输的数据?
反正不是10的倍数……完整的数据就不发了?
自己弄着试试吧
Python使用for循环依次打开该目录下的各文件
import?os
path?=?r"F:\Python\第一周作业\task"
otherpath=r"F:\Python\其它目录"
for?filename?in?os.listdir(path):
????print(path,filename)
????fullname=os.path.join(path,filename)
????if?os.path.isfile(fullname):????????
??????????othername=os.path.join(otherpath,filename)??
??????????otherfile=open(othername,'wb')
??????????for?line?in?open(fullname,'rb'):
??????????????for?c?in?line:
??????????????????if?not?c.isdigit():otherfile.write(c)
??????????otherfile.close()
python 写入excel 遇到缓冲区问题 程序要循环很多次,每次都要读取不同的数据有多也
google搜下吧,python操作excel的模块应该有很多的,你的文件结构还是很清晰的,写个简单parser应该不难。如果你经常要处理这类文本的话,推荐用心写个parser,不要用正则。
你的文件结构差不多是这样,2个等于号行之间是整个序列信息的汇总。
之后----号那行之间是可以扔掉的注释,之后有个n个(optional?)section(section起始于某个有:的行,到下个:行之间终止,都是序列各个元件的成分),我没有py里写parser的经验,不过如果你用parsec的话(这个原生haskell模组有各种语言的binding或类似模组),我恰巧几天前写过一个parsec使用小例子,你如果准备用类似模组,
python 循环读一个文件
Python按行读文件
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-example-2.py
import fileinput
for line in fileinput.input("sample.txt"):
pass
写法简单一些,不过测试以后发现每秒只能读13000行数据,效率比上一种方法慢了两倍多……
3. 带缓存的文件读取
# File: readline-example-3.py
file = open("sample.txt")
while 1:
lines = file.readlines(100000)
if not lines:
break
for line in lines:
pass # do something
这个方法真的更好吗?事实证明,用同样的数据测试,它每秒可以读96900行数据!效率是第一种方法的3倍,第二种方法的7倍!
————————————————————————————————————————————————————————————
在Python 2.2以后,我们可以直接对一个file对象使用for循环读每行数据:
# File: readline-example-5.py
file = open("sample.txt")
for line in file:
pass # do something
而在Python 2.1里,你只能用xreadlines迭代器来实现:
# File: readline-example-4.py
file = open("sample.txt")
for line in file.xreadlines():
pass # do something
python如何实现for循环操作文件?
python用for循环遍历文件操作,代码如下:
#!\urs\bin\env?python
#encoding:utf-8???????#设置编码方式??
import?os
import?re
class?loop_file:
????def?__init__(self,?root_dir,?short_exclude=[],?long_exclude=[],?file_extend=[]):
????????self.root_dir?=?root_dir
????????self.short_exclude?=?short_exclude
????????self.long_exclude?=?long_exclude
????????self.file_extend?=?file_extend
????def?__del__(self):
????????pass
????def?start(self,?func):
????????self.func?=?func
????????return?self.loop_file(self.root_dir)????
????def?loop_file(self,?root_dir):
????????t_sum?=?[]
????????sub_gen?=?os.listdir(root_dir)
????????for?sub?in?sub_gen:
????????????is_exclude?=?False
????????????for?extends?in?self.short_exclude:??##在不检查文件、目录范围中
????????????????if?extends?in?sub:??????????????##包含特定内容
????????????????????is_exclude?=?True
????????????????????break
????????????????if?re.search(extends,?sub):?????##匹配指定正则
????????????????????is_exclude?=?True
????????????????????break????????????????????
????????????if?is_exclude:
????????????????continue????????????
????????????abs_path?=?os.path.join(root_dir,?sub)
????????????is_exclude?=?False
????????????for?exclude?in?self.long_exclude:
????????????????if?exclude?==?abs_path[-len(exclude):]:
????????????????????is_exclude?=?True
????????????????????break
????????????if?is_exclude:
????????????????continue
????????????if?os.path.isdir(abs_path):
????????????????t_sum.extend(self.loop_file(abs_path))
????????????elif?os.path.isfile(abs_path):????????????
????????????????if?not?"."?+?abs_path.rsplit(".",?1)[1]?in?self.file_extend:??##不在后缀名?检查范围中
????????????????????continue
????????????????t_sum.append(self.func(abs_path))
????????return?t_sum????
if?'__main__'==__name__:
????root_dir?=?r'D:\harness\newshoppingcart\testcase\promo\single_promo'
????short_exclude?=?['.svn',?'.*_new.rb']?????###不包含检查的短目录、文件
????long_exclude?=?[]?????????????????????????###不包含检查的长目录、文件
????file_extend?=?['.rb']?????????????????????###包含检查的文件类型
????lf?=?loop_file(root_dir,?short_exclude,?long_exclude,?file_extend)
????for?f?in?lf.start(lambda?f:?f):
????????print?f
python中要求读取一系列文件,x1.txt,x2.txt……直到不存在xn.txt为止,程序的循环条件应该怎么写?
如果文件名中的序号是连续的,他们提供的方法都可以。
如果不连续,如不存在 x3.txt,但 x4.txt 又存在,则 x4.txt 及其后面的文件程序都不会读取。
比较好的办法是提供 x1.txt,x2.txt…… 所在的目录,再获取目录中的文件,再读取。