Python中的CSV文件处理
我使用以下格式输出到文本文件的空间数据:Python中的CSV文件处理,python,file,csv,Python,File,Csv,我使用以下格式输出到文本文件的空间数据: COMPANY NAME P.O. BOX 999999 ZIP CODE , CITY +99 999 9999 23 April 2013 09:27:55 PROJECT: Link Ref -------------------------------------------------------------------------------- -----------------------------------------------
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
问题:
- 如何让以“点编号”开头的行成为输出文件的标题?我试图用DictReader代替reader/writer位,但无法使其工作
- 使用分隔符“”写入输出文件确实有效,但会在每个空格处写入一个逗号,使输出文件中的空列过多。我该如何避免这种情况
- 如何删除页脚
编写器;所以你只会得到最后一个
您的代码可以是这样的,不需要CSV阅读器或编写器,因为它足够简单,可以解析为简单的文本(如果您有文本列,带有转义字符等等,就会出现问题)
回答您的第一个和最后一个问题:这只是关于忽略相应的行,即不将它们写入输出。这对应于fortran建议的如果未找到标题
和否则如果未找到行:
块
第二点是,您的文件中没有专用的分隔符:您有一个或多个空格,这使得使用csv
module解析很困难。使用split()
#! /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)
感谢您的快速回答。请使用elif
而不是else,如果使用+1来指示转义字符,这可能是一个内置函数难以处理的问题。请单击答案左侧的大勾线框,奖励为您提供最佳ID的用户,好吗?
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)
#! /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)