2015-09-28 38 views
1

如何通过(Line)和(point)对文件进行排序,然后检查“Line”列==下一行“Line”列。在文本文件中比较2行中的1列

所以基本上如何获得下一个值,并将其与当前值进行比较按2键排序后。

我有这个文本文件:

T,Swath,Line,Point,Idx,I,J,X,Y,Initial X,Initial Y,State 
SP,1915,75501,64233.00,1,64233,75501,677912.500,3093762.500,677912.500,3093762.500,Theoretic 
SP,1915,75501,64243.00,1,64243,75501,678037.500,3093762.500,678037.500,3093762.500,Theoretic 
SP,1915,75501,64253.00,1,64253,75501,678162.500,3093762.500,678162.500,3093762.500,Theoretic 
SP,1915,75501,64263.00,1,64263,75501,678287.500,3093762.500,678287.500,3093762.500,Theoretic 
SP,1915,75501,64273.00,1,64273,75501,678412.500,3093762.500,678412.500,3093762.500,Theoretic 
SP,1915,75501,64283.00,1,64283,75501,678537.500,3093762.500,678537.500,3093762.500,Theoretic 
SP,1915,75501,64293.00,1,64293,75501,678662.500,3093762.500,678662.500,3093762.500,Theoretic 
SP,1915,75501,64303.00,1,64303,75501,678787.500,3093762.500,678787.500,3093762.500,Theoretic 
SP,1915,75501,64313.00,1,64313,75501,678912.500,3093762.500,678912.500,3093762.500,Theoretic 
SP,1915,75501,64323.00,1,64323,75501,679037.500,3093762.500,679037.500,3093762.500,Theoretic 
SP,1915,75501,64333.00,1,64333,75501,679162.500,3093762.500,679162.500,3093762.500,Theoretic 
SP,1915,75501,64343.00,1,64343,75501,679287.500,3093762.500,679287.500,3093762.500,Theoretic 
SP,1915,75501,64353.00,1,64353,75501,679412.500,3093762.500,679412.500,3093762.500,Theoretic 
SP,1915,75501,64363.00,1,64363,75501,679537.500,3093762.500,679537.500,3093762.500,Theoretic 

,这是我的代码:

Fin = open("1891_2150.txt" , "r") 
for line in Fin: 
     if line.startswith("T"): # to skip the header 
     print ("\n ..\n") 
     else: 
     line2 = line.split(",") 
     LineNb = int(float(line2[2])) 
     PointNb = int(float(line2[3])) 
     iGrid = int(line2[5]) 
     jGrid = int(line2[6]) 
     X = float(line2[7]) 
     Y = float(line2[8]) 
     iX = float(line2[9]) 
     iY = float(line2[10]) 
     if LineNb == next(LineNb): 
      Dx = X - next(x) 
      print (Dx) 

非常感谢

回答

0
from operator import itemgetter 
from itertools import groupby 

def convert(row): # convert each row to a list 
    lst = row.split(",") 
    return lst 


with open("1891_2150.txt" , "r") as f: 
    content = f.readlines() 

content = content[1:] # remove the file head 
rows = map(convert,content) 

sorted(rows, key=itemgetter(2)) # sort by the Line 


for key, group in groupby(rows, lambda x: x[2]): 
    sorted(group,key=itemgetter(3)) #sort by the Point 

for index in range(len(rows) - 1): 
    if rows[index][2] == rows[index+1][2]: 
     X = float(rows[index][7]) 
     X_next = float(rows[index + 1][7]) 
     print X - X_next 
+0

TypeError:'map'类型的对象没有len()..我得到这个错误 –

+0

请发布您的TypeError回溯。按点添加排序 – Hooting

+0

用于范围内的索引(len(rows) - 1): TypeError:'map'类型的对象没有len() –

0

如果这样做的主要目的是,当检测到这些值在连续的行中是相同的,您可以将当前值与前一个值进行比较。

另外你可能想看看Python的csv模块。

import csv 
import copy 

columns = [] 
with open('data.txt', 'r') as data: 
    data_reader = csv.reader(data, delimiter=',') 
    previous_value = None 
    columns_numbers = [] 
    for row in data_reader: 
     if row[2] == previous_value: 
      if len(columns_numbers) == 0: 
       columns_numbers.extend([data_reader.line_num - 1, data_reader.line_num]) 
      else: 
       columns_numbers.append(data_reader.line_num) 
     else: 
      if len(columns_numbers): columns.append(copy.copy(columns_numbers)) 
      columns_numbers = [] 
     previous_value = row[2] 

    if len(columns_numbers): 
     columns.append(copy.copy(columns_numbers)) 

print(columns) 

使用数据样本作为输入将是此代码的输出中:

[[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]] 

这意味着从2到15行具有用于列Line相同的值。