简体   繁体   English

使用Python或Python / Pandas将具有.fmt文件的固定宽度的.dat文件以编程方式转换为.csv文件

[英]Programically convert a fixed width .dat file with a .fmt file into a .csv file using Python or Python/Pandas

I'm trying to learn Python but I'm stuck here, any help appreciated. 我正在尝试学习Python,但是我被困在这里,不胜感激。

I have 2 files. 我有2个档案。

1 is a .dat file with no column headers that is fixed width containing multiple rows of data 1 is a .fmt file that contains the column headers, column length, and datatype 1是没有固定宽度包含多行数据的列标题的.dat文件1是是包含列标题,列长和数据类型的.fmt文件

.dat example: .dat示例:

10IFKDHGHS34
12IFKDHGHH35
53IFHDHGDF33

.fmt example: .fmt示例:

ID,2,n
NAME,8,c
CODE,2,n

Desired Output as .csv: 所需的输出为.csv:

ID,NAME,CODE
10,IFKDHGHS,34
12,IFKDHGHH,35
53,IFHDHGDF,33

First, I'd parse the format file. 首先,我将分析格式文件。

with open("format_file.fmt") as f:
    # csv.reader parses away the commas for us 
    # and we get rows as nice lists
    reader = csv.reader(f) 
    # this will give us a list of lists that looks like
    # [["ID", "2", "n"], ...]
    row_definitions = list(reader)

# iterate and just unpack the headers
# this gives us ["ID", "NAME", "CODE"]
header = [name for name, length, dtype in row_definitions]
# [2, 8, 2]
lengths = [int(length) for name, length, dtype in row_definitions]

# define a generator (possibly as a closure) that simply slices 
# into the string bit by bit -- this yields, for example, first 
# characters 0 - 2, then characters 2 - 10, then 8 - 12
def parse_line(line):
    start = 0
    for length in lengths:
        yield line[start: start+length]
        start = start + length

with open(data_file) as f:
     # iterating over a file pointer gives us each line separately
     # we call list on each use of parse_line to force the generator to a list
     parsed_lines = [list(parse_line(line)) for line in data_file]

# prepend the header
table = [header] + parsed_lines

# use the csv module again to easily output to csv
with open("my_output_file.csv", 'w') as f:
    writer = csv.writer(f)
    writer.writerows(table) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM