比较文本文件中2行中的1列

时间:2015-09-28 11:33:04

标签: python

如何按(Line)和(point)对文件进行排序,然后检查“Line”列==下一行“Line”列。

基本如何获取下一个值并将其与当前值进行比较按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)

非常感谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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具有相同的值。