使用固定格式文本db [英] Working with fixed format text db's

查看:96
本文介绍了使用固定格式文本db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须处理的许多文件格式都是所谓的

固定格式记录,其中文件中的每一行都是一条记录,

和每一种记录中的字段占用特定的空间。


例如,我的一个旧的Python程序包含

后创建固定格式一批新学生的文本记录

学生:


new = file(" new.dat"," w")

如果不是新的:

print"错误。无法打开文件new.dat进行写入。

raw_input("按返回退出。)

sys.exit(1)


for s新生:

new.write(s.ssn.ljust(9))

new.write(s.id。 ljust(10))

new.write(s.last [:16] .ljust(16))

new.write(s.first [:11]。 ljust(11))

new.write(''''。ljust(10))#Phone Number

new.write(''''。ljust(1254) ))#Empty''填充''空格。

new.write('''2813'')

new.write(s.major.ljust(5))


#等...


幸运的是,输出格式还没有改变,所以

的问题保持上述不会出现。


但是,我想要更好的东西。


是否已有一个很好的模块使用固定格式

可用的记录?我找不到一个。


如果没有,请建议我如何改进上述代码。


-

Neil Cerutti

当渴望时唱完了,表演者听起来像是对他们的渴望。 - 音乐点亮论文

解决方案

Neil Cerutti< ho ***** @ yahoo.comwrote:


幸运的是,输出格式还没有改变,所以

维持上述问题并没有出现。



问题当然是当你想要改变你必须做的格式

所以在所有文件中(以及备份怎么样)然后?)和所有程序

同时。


维护代码是你问题中最少的,我会说。


您可以更改数据布局,以便例如每个字段由

标记字符终止,然后读/写分隔值。但除非你还要检查你的程序的所有其他部分,否则你需要确保你

在任何隐含地依赖于它的地方都没有任何其他代码。特别是

字段是已知的固定长度。


>

但是,我想要一些东西更好。



你想要达到什么目的?

-

Jeremy CB Nicoll - 我的意见是我的拥有。


2007-06-08,Jeremy CB Nicoll< je **** @ omba.demon.co.ukwrote:


Neil Cerutti< ho ***** @ yahoo.comwrote:


>幸运的是,输出格式没有改变然而,
保持上述问题并没有出现。



问题肯定是当你想要改变格式

时你必须在所有文件中这样做(以及备份怎么办?

然后?)和所有程序同时进行。



遗憾的是,我无法控制格式。这是一个商业数据库应用程序的导入

文件格式。


维护代码是你问题中最少的,我''

说。


您可以更改数据布局,例如每个字段是

由标记字符终止,然后读/写分隔

值。但除非您还要查看

程序的所有其他部分,否则您需要确保在任何隐含地依赖于它的地方都没有任何其他的

代码。特定领域

是一个已知的固定长度。


>但是,我想要更好的东西。



你想要达到什么目的?



我希望有一个模块为我提供了一种指定

固定文件格式的方法,以及某种界面编写

并读取所述格式的文件。


用ad-hoc代码执行此操作实际上并不困难,但随后

该程序无法通过

手中的规范硬拷贝无法辨认。而且,正如你所说的,如果规范确实发生了变化,那么手写的一批ljust,rjust和slice将会有一些重新配置的痛苦。< br $> b $ b但对我而言,最大的弱点是规范不在代码中,或者代码读取和使用,我认为它应该是。


如果什么都不存在,我想我会自己动手。但我希望

更加懒散,而且几乎所有已发布的模块都比我自己写的更好。 ;)


当然,根本问题是具有固定宽度数据字段的古老平面文件

格式。即使是教育部的大部分数据文件已经转移到了XML,这对我来说更简单。

更容易解析。
< br $>
-

Neil Cerutti


在< sl ************ ********@FIAD06.norwich.edu> ;, Neil Cerutti写道:


new = file(" new.dat"," w" ;)

如果不是新的:

print"错误。无法打开文件new.dat进行写入。

raw_input("按Return To Exit。)

sys.exit(1)



嘿,Python不是C.文件对象应该*总是*是真。错误

是通过例外处理的。


Ciao,

Marc''BlackJack''Rintsch


Many of the file formats I have to work with are so-called
fixed-format records, where every line in the file is a record,
and every field in a record takes up a specific amount of space.

For example, one of my older Python programs contains the
following to create a fixed-format text record for a batch of new
students:

new = file("new.dat", "w")
if not new:
print "Error. Could not open file new.dat for writing."
raw_input("Press Return To Exit.")
sys.exit(1)

for s in freshmen:
new.write(s.ssn.ljust(9))
new.write(s.id.ljust(10))
new.write(s.last[:16].ljust(16))
new.write(s.first[:11].ljust(11))
new.write('' ''.ljust(10)) # Phone Number
new.write('' ''.ljust(1254)) # Empty ''filler'' space.
new.write(''2813 '')
new.write(s.major.ljust(5))

# Etc...

Luckily, the output format has not changed yet, so issues with
maintaining the above haven''t arisen.

However, I''d like something better.

Is there already a good module for working with fixed format
records available? I couldn''t find one.

If not, please suggest how I might improve the above code.

--
Neil Cerutti
When "yearn" was sung, the performers ounded like they were in a state of
yearning. --Music Lit Essay

解决方案

Neil Cerutti <ho*****@yahoo.comwrote:

Luckily, the output format has not changed yet, so issues with
maintaining the above haven''t arisen.

The problem surely is that when you want to change the format you have to do
so in all files (and what about the backups then?) and all programs
simultaneously.

Maintaining the code is the least of your the problems, I''d say.

You could change the data layout so that eg each field was terminated by a
marker character, then read/write delimited values. But unless you also
review all the other parts of your programs, you need to be sure that you
don''t have any other code anywhere that implicitly relies on a particular
field being a known fixed length.

>
However, I''d like something better.

What precisely do you want to achieve?
--
Jeremy C B Nicoll - my opinions are my own.


On 2007-06-08, Jeremy C B Nicoll <je****@omba.demon.co.ukwrote:

Neil Cerutti <ho*****@yahoo.comwrote:

>Luckily, the output format has not changed yet, so issues with
maintaining the above haven''t arisen.


The problem surely is that when you want to change the format
you have to do so in all files (and what about the backups
then?) and all programs simultaneously.

I don''t have control of the format, unfortunately. It''s an import
file format for a commercial database application.

Maintaining the code is the least of your the problems, I''d
say.

You could change the data layout so that eg each field was
terminated by a marker character, then read/write delimited
values. But unless you also review all the other parts of your
programs, you need to be sure that you don''t have any other
code anywhere that implicitly relies on a particular field
being a known fixed length.

>However, I''d like something better.


What precisely do you want to achieve?

I was hoping for a module that provides a way for me to specify a
fixed file format, along with some sort of interface for writing
and reading files that are in said format.

It is not actually *hard* to do this with ad-hoc code, but then
the program is indecipherable without a hardcopy of the spec in
hand. And also, as you say, if the spec ever does change, the
hand-written batch of ljust, rjust and slice will be somewhat of
a pain to reconfigure.

But biggest weakness, to me, is that the specification is not in
the code, or read and used by the code, and I think it should be.

If nothing exists already I guess I''ll roll my own. But I''d like
to be lazier, and virtually all published modules are better than
what I''ll write for myself. ;)

The underlying problem, of course, is the archaic flat-file
format with fixed-width data fields. Even the Department of
Education has moved on to XML for most of it''s data files, which
are much simpler for me to parse.

--
Neil Cerutti


In <sl********************@FIAD06.norwich.edu>, Neil Cerutti wrote:

new = file("new.dat", "w")
if not new:
print "Error. Could not open file new.dat for writing."
raw_input("Press Return To Exit.")
sys.exit(1)

Hey, Python is not C. File objects should *always* be "true". An error
is handled via exceptions.

Ciao,
Marc ''BlackJack'' Rintsch


这篇关于使用固定格式文本db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆