Skip to main content
 首页 » 编程设计

python之删除特定属性小于或等于 0 的行

2025年05月04日1398°冷暖

我有几个大文本文件(超过 3000 万行,>1GB),它们在拆分后正在 ArcGIS 中处理(请参阅 Remove specific lines from a large text file in python 和 chunk a text database into N equal blocks and retain header 了解背景信息)。

即使在拆分过程后也需要 3 天以上,所以我想删除所有 (Rx) 值小于或等于 0 的 xy 点。

我没有 python 来读取超过 500Mb 的 txt 数据集,所以我使用 cygwin/SED 命令对数据进行初始清理,然后使用 python 对文件进行分块。所以理想情况下,该过程是向 python 添加一些代码(见下文)以不包括所有 Rx<=0 的行。

Latitude    Longitude   Rx  Best_Unit 
-16.37617    144.68805  -012.9  7 
-16.37617    144.68834  -015.1  7 
-16.37617    144.68861  -017.2  7 
-16.37617    144.68890  -018.1  7 
-16.37617    144.68919  -025.0  7 
-16.37617    144.68945  -019.5  7 
-16.37617    144.68974  -020.0  7 
-16.37617    144.69003  -020.4  7 
-16.37617    144.69623   015.3  7 
-16.37617    144.69652   015.6  7 
-16.37617    144.69679   015.8  7 
-16.37617    144.69708   016.0  7 
-16.37617    144.70076   005.0  7 
-16.37617    144.70103   002.2  7 
-16.37617    144.70131  -000.2  7 
-16.37617    144.70160  -001.5  7 
-16.37617    144.70187  -001.0  7 
-16.37617    144.70216   000.7  7 
-16.37617    144.70245   002.2  7 
-16.37617    144.70273   008.4  7 
-16.37617    144.70300   017.1  7 
-16.37617    144.70329   017.2  7 

我希望将 Rx>0 的所有行(行)写入一个新的文本文件。我还想删除 Best_Unit 列。

from itertools import islice 
 
import arcpy, os 
#fc = arcpy.GetParameter(0) 
#chunk_size = arcpy.GetParameter(1) # number of records in each dataset 
 
fc='cb_vhn007_5.txt' 
Name = fc[:fc.rfind('.')] 
fl = Name+'.txt' 
 
headers_count = 1 
chunk_size = 500000 
 
with open(fl) as fin: 
  headers = list(islice(fin, headers_count)) 
 
  part = 1 
  while True: 
    line_iter = islice(fin, chunk_size) 
    try: 
      first_line = line_iter.next() 
    except StopIteration: 
      break 
    with open(Name+'_%d.txt' % part, 'w') as fout: 
      for line in headers: 
        fout.write(line) 
      fout.write(first_line) 
      for line in line_iter: 
         ## add something here to check if value after third tab 
         ## is >0 and if so then write the row or skip. 
        fout.write(line)  
 
    print "Created part %d" % part 
    part += 1 

新代码 - 第一行包括 - Rx 值。

from itertools import islice 
 
import arcpy, os 
#fc = arcpy.GetParameter(0) 
#chunk_size = arcpy.GetParameter(1) # number of records in each dataset 
 
fc='cb_vhn007_5.txt' 
Name = fc[:fc.rfind('.')] 
fl = Name+'.txt' 
 
headers_count = 1 
chunk_size = 500000 
 
with open(fl) as fin: 
  headers = list(islice(fin, headers_count)) 
 
  part = 1 
  while True: 
    line_iter = islice(fin, chunk_size) 
    try: 
      first_line = line_iter.next() 
    except StopIteration: 
      break 
    with open(Name+'_%d.txt' % part, 'w') as fout: 
      for line in headers: 
        fout.write(line) 
      fout.write(first_line) 
      for line in line_iter: 
        if line.split()[2][0:1] != '-': 
          #print line.split()[2] 
          fout.write(line) 
 
    print "Created part %d" % part 
    part += 1 

请您参考如下方法:

您可以使用 line.split() 将您的行拆分为一个列表,其中包含 4 列中每一列的值。

例如:

line='-16.37617\t144.70329\t017.2\t7' 
line.split() 
# ['-16.37617', '144.70329', '017.2', '7'] 

然后您可以将 line[2](记住 python 是基于 0 的索引)强制转换为一个数字并检查它是否 > 0:

if float(line.split()[2]) > 0: 
    fout.write(line) 

或者您可以只检查它是否有减号:

if line.split()[2].find('-') != -1: 
    fout.write(line) 

如果您的列对于每个文本文件的顺序可能不同,您可以对标题执行 split(),确定哪个是 Rx,然后使用它而不是 2:

i = headers.split().index('Rx') 
# now use line[i]