python - 难以使用具有不同格式字符串的 Python struct.Struct.unpack_from

标签 python c parsing struct buffer

第一次海报,长期潜伏者。已经到处寻找答案,但已经到了那个阶段......!

我在实现 John Machin 对过去这个问题的回答时遇到了一些麻烦:

How to efficiently parse fixed width files?

在非常高的层次上,我使用这段代码来拆分固定格式的文本文件并将它们导入到 PostgreSQL 数据库中。我已经成功地使用这段代码实现了一个文本文件的解决方案,但是我现在试图扩展我的程序以处理具有不同固定格式的不同文本文件,并且不断遇到相同的错误:

struct.error: unpack_from requires a buffer of at least [x] bytes

当然,根据我提供给函数的格式字符串,我得到了不同的 x 值 - 我的问题是它继续适用于一种且仅适用于一种格式,而不适用于任何其他格式。我唯一要更改的是用于计算格式字符串的变量,以及脚本中与格式相关的变量名称。

例如,这很好用:

cnv_text = lambda s: str(s.strip())
cnv_int = lambda s: int(s) if s.isspace() is False else s.strip()
cnv_date_ymd = lambda s: datetime.datetime.strptime(s, '%Y%m%d') if s.isspace() is False else s.strip() # YYYY-MM-DD

unpack_len = 0
unpack_fmt = ""
splitData = []

conn = psycopg2.connect("[connection info]")
cur = conn.cursor()

Table1specs = [
    ('A', 6, 14, cnv_text),
    ('B', 20, 255, cnv_text),
    ('C', 275, 1, cnv_text),
    ('D', 276, 1, cnv_text),
    ('E', 277, 1, cnv_text),
    ('F', 278, 1, cnv_text),
    ('G', 279, 1, cnv_text),
    ('H', 280, 1, cnv_text),
    ('I', 281, 8, cnv_date_ymd),
    ('J', 289, 8, cnv_date_ymd),
    ('K', 297, 8, cnv_date_ymd),
    ('L', 305, 8, cnv_date_ymd),
    ('M', 313, 8, cnv_date_ymd),
    ('N', 321, 1, cnv_text),
    ('O', 335, 2, cnv_text),
    ('P', 337, 2, cnv_int),
    ('Q', 339, 5, cnv_int),
    ('R', 344, 255, cnv_text),
    ('S', 599, 1, cnv_int),
    ('T', 600, 1, cnv_int),
    ('U', 601, 5, cnv_int),
    ('V', 606, 10, cnv_text)
    ]

#for each column in the spec variable...
for column in Table1specs:
    start = column[1] - 1
    end = start + column[2]
    if start > unpack_len:
    unpack_fmt += str(start - unpack_len) + "x"
    unpack_fmt += str(end - start) + "s"
unpack_len = end
field_indices = range(len(Table1specs))
print unpack_len, unpack_fmt
#set unpacker
unpacker = struct.Struct(unpack_fmt).unpack_from

class Record(object):
    pass

filename = "Table1Data.txt"

f = open(filename, 'r')
for line in f:
    raw_fields = unpacker(line)
    r = Record()
    for x in field_indices:
        setattr(r, Table1specs[x][0], Table1specs[x][3](raw_fields[x]))
    splitData.append(r.__dict__)

所有数据都附加到 splitData,然后我在一个循环中循环并处理 SQL 语句以通过 psycopg2 输入到数据库中。当我将规范更改为其他内容(以及其他变量也反射(reflect)这一点)时,我收到错误。它是从“raw_fields = unpacker(line)”行抛出的。

我已经用尽了所有的资源,现在束手无策......欢迎任何想法或想法。

(会不会是我导入的文本文件的问题?)

最好的问候。

最佳答案

后来解决了这个问题:问题是由我正在解析的文本文件引起的。这些行不够长,所以我编写了一个函数,在每行的末尾写入空格以使其长度正确:

def checkLineLength(checkFile, minLength):
    print ('Checking length of lines in file '+ checkFile+', where minimum line length is '+str(minLength))
    counter = 0
    fixedFile = 'fixed'+checkFile
    src = open(checkFile, 'r')
    dest = open(fixedFile, 'w')
    lines = src.readlines()
    for line in lines:
        if len(line) < minLength:
            x = (line.rstrip('\r\n') + (" "*(minLength-(len(line)-1))+'\r\n'))
            dest.write(x)
            counter += 1
        else:
            dest.write(line)
    if counter > 0:
        os.remove(checkFile)
        os.rename(fixedFile, checkFile)
        print (str(counter) + " lines fixed in "+ checkFile)

else:
    print('Line length in '+checkFile+' is fine.' )
    os.remove(fixedFile)

关于python - 难以使用具有不同格式字符串的 Python struct.Struct.unpack_from,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22563442/

相关文章:

将字符串中的字符更改为两个字符

c - 如何消除有关 __FUNCTION__ 的 GCC 迂腐 (-Wpedantic) 警告

android - 在 Webview Activity 中打开 RSS 链接

python - 使用 Python 在 Raspberry Pi USB 端口上使用 uart

python - 我希望所有三个形状都在同一条线上...请帮忙...!

python - wxpython:检测对话框是否已关闭

Python装饰器多次调用函数

c - 错误结果私有(private)数组 openmp

python - 从日志文件中提取唯一访问者列表

javascript - javascript中千位分隔符分割数字