Python:文件对象作为函数参数 [英] Python: file object as function argument

查看:124
本文介绍了Python:文件对象作为函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在要导入到主脚本中的模块中编写了一个函数( read()):该函数只是读取带有正则表达式的文件并从中创建一个数组.

I have written a function (read()) in a module I want to import in my main script: this function simply reads a file with Regular Expressions and creates an array from it.

此函数唯一需要读取的参数是文件(.txt,仅数字).我想在主脚本中打开文件 data = open('output99.txt','r'),为文件提供对象 data ,然后传递文件对象 data 到函数中,以便我可以随时更改输入文件.这可能是个问题吗?因为如果这样做,该函数将无法工作,并返回一个空数组:

The only parameter this function takes is the file (.txt, only numbers) it has to read. I would like to open the file in my main script data = open('output99.txt', 'r') giving the file the object data and then pass the file object data to the function, so that I can change the input file whenever I want..could that be a problem? Because if I do so, the function doesn't work, and returns an empty array:

def read(data):

n_lines = sum(1 for line in data)
array = np.empty((n_lines,13))
re_re = re.compile('^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)')

i=0
for line in data:
    reg = re_re.search(line)
    if(re!=None):
        array[i,0] = reg.group(1)
        array[i,1] = reg.group(2)
        array[i,2] = reg.group(3)
        array[i,3] =  reg.group(4)
        array[i,4] =  reg.group(5)  
        array[i,5] = reg.group(6)
        array[i,6] = reg.group(7)
        array[i,7] = reg.group(8)
        array[i,8] = reg.group(9)       
        array[i,9] = reg.group(10)
        array[i,10] = reg.group(11)
        array[i,11] = reg.group(12)
        array[i,12] = reg.group(13)

    i+=1



return array

如果我在函数中打开文件,它可以正常工作,但是当我想更改输入文件时,速度会大大降低.谁能解释给我听?

If I open the file inside the function, it works correctly, but it would be far less quick when i want to change input file. Can anyone explain me that?

推荐答案

data 不是文件名;这是一个 file 对象.当您使用

data is not a file name; it's a file object. When you read from it using

sum(1 for line in data)

您读取了文件的全部内容,因此文件指针位于文件的末尾.当您尝试再次使用

you read the entire contents of the file, so that the file pointer is at the end of the file. When you try to read from it again with

for line in data:

您没有任何数据,因为您已经读取了文件中的所有内容.要解决此问题,您必须在计数行数后用

you get no data, because you've already read everything in the file. To fix, you'll have to reset the file pointer after you count the number of lines with

data.seek(0)

这篇关于Python:文件对象作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆