Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读取文件和处理具有相同ID的元素_Python - Fatal编程技术网

Python 读取文件和处理具有相同ID的元素

Python 读取文件和处理具有相同ID的元素,python,Python,因此,我想逐行处理下面给出的示例文件,并一次性处理具有相同ID(下面文件中的1,2,3,7)的所有记录,然后在处理后继续处理下一个ID。我编写了一些代码,在该文件中读取一次,读取第一个ID,将其存储在变量中,然后关闭该文件。在此之后,我再次打开该文件并逐行处理它 testprogdata.csv 10,xyz,abx 10,mno,mnc 10,mnp,klo 10,apl,lko 2,pol,okl 3,jkl,mlp 3,jsd,sdf 3,fds,lpo 7,iko,opi 用于读取上

因此,我想逐行处理下面给出的示例文件,并一次性处理具有相同ID(下面文件中的1,2,3,7)的所有记录,然后在处理后继续处理下一个ID。我编写了一些代码,在该文件中读取一次,读取第一个ID,将其存储在变量中,然后关闭该文件。在此之后,我再次打开该文件并逐行处理它

testprogdata.csv

10,xyz,abx
10,mno,mnc
10,mnp,klo
10,apl,lko
2,pol,okl
3,jkl,mlp
3,jsd,sdf
3,fds,lpo
7,iko,opi
用于读取上述文件的Python程序:

file=open('testprogdata.csv')
for line in file:
    sL=line.split(',')
    en=sL[0]
    break
file.close()

file=open('testprogdata.csv')

for line in file:
    sL=line.split(',')
    x=sL[0]
    if(x==en):
        print x //Do something processing 
    else:
        #process(x)
        en=x
        print "-----------"
        print x


Output

10
10
10
10
----------
2
----------
3
3
3
----------
7

现在我对我编写的代码感到不舒服,因为对于一个简单的问题来说,它似乎是一种糟糕的编程模式。实现上述常见模式的好方法是什么?即使我使用散列,我也将执行上述操作。

我将读取具有相同id的所有行,处理并再次重复:

f=open('testprogdata.csv', "r")
id_to_process = 1
lines_to_process = []

while True:
   raw_line = f.readline()

   # check whether we're at eof       
   if not raw_line:
        break

   # get line
   line = raw_line.rstrip().split(",")
   id = line[0]

   # same id
   if id == id_to_process:
       lines_to_process.append(line)

   # a new id is comming
   else:
       # do something with your lines
       foobar(lines_to_process)

       # now proceed with next id
       id_to_process = id
       lines_to_process = [line]

f.close()

但请注意:代码需要一组行列表。否则,以这种方式处理它们是没有意义的。

我将读取具有相同id的所有行,处理并再次重复:

f=open('testprogdata.csv', "r")
id_to_process = 1
lines_to_process = []

while True:
   raw_line = f.readline()

   # check whether we're at eof       
   if not raw_line:
        break

   # get line
   line = raw_line.rstrip().split(",")
   id = line[0]

   # same id
   if id == id_to_process:
       lines_to_process.append(line)

   # a new id is comming
   else:
       # do something with your lines
       foobar(lines_to_process)

       # now proceed with next id
       id_to_process = id
       lines_to_process = [line]

f.close()

但请注意:代码需要一组行列表。否则,以这种方式处理它们是没有意义的。

一种类似python的方式是使用,并且:


一种类似蟒蛇的方法是使用,以及:


你的档案有多大?您是否愿意阅读整个内容,然后进行处理?该文件的ID按升序排列?@Abhijit谢谢!我只是想知道在这两种情况下,1)文件什么时候太大,我想逐行读取&还有2)它什么时候可以立即放入内存(小文件)。正如我所想,这应该是许多程序员无论使用何种语言都可能遇到的一种非常常见的模式。@Nigeltuffel谢谢!不,这都是随机的。用随机数字编辑了上面的问题。你的文件有多大?您是否愿意阅读整个内容,然后进行处理?该文件的ID按升序排列?@Abhijit谢谢!我只是想知道在这两种情况下,1)文件什么时候太大,我想逐行读取&还有2)它什么时候可以立即放入内存(小文件)。正如我所想,这应该是许多程序员无论使用何种语言都可能遇到的一种非常常见的模式。@Nigeltuffel谢谢!不,这都是随机的。用随机数编辑了上面的问题。