第一次海报,长时间潜伏。到处寻找答案,但已经到了那个阶段……!
我在执行约翰·马钦对过去问题的回答时遇到了一些困难:
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输入数据库。当我将规范更改为其他内容(以及其他变量也反映了这一点)时,我就会收到错误。它从'raw_fields = unpacker( line )‘行抛出。
我已经用尽了所有的资源,而且我没有把握好.任何受欢迎的想法或想法。
(这是否与我从其中导入的文本文件有关?)
诚挚的问候。
发布于 2014-03-26 14:08:55
已经解决了这个问题:问题是由我解析的文本文件引起的。这些行不够长,因此我编写了一个函数,将空格写入每一行的末尾,以使它们具有正确的长度:
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)https://stackoverflow.com/questions/22563442
复制相似问题