Python中的CSV文件处理

2024-06-02 12:47:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的空间数据输出为以下格式的文本文件:

COMPANY NAME
P.O. BOX 999999
ZIP CODE , CITY 
+99 999 9999
23 April 2013 09:27:55

PROJECT: Link Ref
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Design DTM is 30MB 2.5X2.5
Stripping applied to design is 0.000

Point Number      Easting     Northing        R.L. Design R.L.  Difference  Tol  Name
     3224808   422092.700  6096059.380       2.520     -19.066     -21.586  --   
     3224809   422092.200  6096059.030       2.510     -19.065     -21.575  --   
<Remainder of lines>
 3273093   422698.920  6096372.550       1.240     -20.057     -21.297  --   

Average height difference is -21.390
RMS  is  21.596
0.00 % above tolerance
98.37 % below tolerance
End of Report

如图所示,文件有页眉和页脚。数据由空格分隔,但列之间的分隔量不相等。在

我需要的是逗号分隔的文件,包括东距、北距和差分。在

我不想手工修改几百个大文件,我正在写一个小脚本来处理这些文件。到目前为止,我得到的是:

^{pr2}$

问题:

  • 如何让以“Point number”开头的行成为输出文件的头?我试图用DictReader来代替读写器,但无法使其工作。

  • 用分隔符''编写输出文件可以工作,但是在每个空格处写一个逗号,在我的输出文件中留下了太多的空列。我该如何避开这个问题?

  • 如何删除页脚?


Tags: 文件ofnameboxis格式tolerancecompany
3条回答

这起作用了:

#! /usr/bin/env python

import glob,os

list_of_files = glob.glob('C:/test/*.txt')

def process_file(source, dest):
  header_found = False
  for line in source:
    line = line.strip()
    if not header_found:
      #ignore everything until we find this text
      header_found = line.startswith('Stripping applied') #otherwise, header is lost
    elif not line:
      return #we are done when we find an empty line
    else:
      #write the needed columns
      columns = line.split()
      dest.writelines(','.join(columns[i] for i in (1, 2, 5))+"\n") #newline character adding was necessary

for filename in list_of_files:
  short_filename, extension = os.path.splitext(filename)
  file_out_name = short_filename + '_ed' + ".csv"
  with open(filename, 'r') as source:
    with open(file_out_name, 'wb') as dest:
      process_file(source, dest)

回答你的第一个和最后一个问题:这只是关于忽略相应的行,即不把它们写入输出。这对应于fortran建议的if not header_found和{}块。在

第二点是文件中没有专用的分隔符:您有一个或多个空格,这使得使用csv模块很难进行解析。使用split()将解析每一行并返回非空字符列表,因此只返回有用的值。在

我可以看到您的代码有一个问题,您正在为每一行创建一个新的writer;因此您将只使用最后一行。在

您的代码可以是这样的,不需要CSV读取器或编写器,因为它足够简单,可以被解析为简单的文本(如果您有文本列,带有转义字符等等,就会出现问题)。在

def process_file(source, dest):
  found_header = False
  for line in source:
    line = line.strip()
    if not header_found:
      #ignore everything until we find this text
      header_found = line.starswith('Point Number')
    elif not line:
      return #we are done when we find an empty line, I guess
    else:
      #write the needed columns
      columns = line.split()
      dest.writeline(','.join(columns[i] for i in (1, 2, 5)))

for filename in list_of_files:
  short_filename, extension = os.path.splitext(filename)
  file_out_name = short_filename + '_ed' + extension
  with open(filename, 'r') as source:
    with open(file_out_name. 'w') as dest:
      process_file(source, dest)

相关问题 更多 >