编写/解析具有固定宽度行的文本文件
我是 Python 的新手,我正在考虑使用它来编写我们的供应商需要的一些复杂的 EDI 内容。
基本上,他们需要一个 80 个字符的固定宽度文本文件,其中某些字段“块”包含数据,而其他字段留空。 我有文档,所以我知道每个“块”的长度是多少。 我得到的响应更容易解析,因为它已经有了数据,我可以使用 Python 的“切片”来提取我需要的内容,但我无法分配给切片 - 我已经尝试过,因为它听起来不错解决方案,但它不起作用,因为 Python 字符串是不可变的:)
就像我说的,我确实是 Python 的新手,但我很高兴学习它:) 我将如何去做呢? 理想情况下,我希望能够说范围 10-20 等于“Foo”,并让它成为带有 7 个附加空白字符的字符串“Foo”(假设所述字段的长度为 10),并将其设为更大的 80 个字符字段的一部分,但我不确定如何做我的想法。
I'm a newbie to Python and I'm looking at using it to write some hairy EDI stuff that our supplier requires.
Basically they need an 80-character fixed width text file, with certain "chunks" of the field with data and others left blank. I have the documentation so I know what the length of each "chunk" is. The response that I get back is easier to parse since it will already have data and I can use Python's "slices" to extract what I need, but I can't assign to a slice - I tried that already because it sounded like a good solution, and it didn't work since Python strings are immutable :)
Like I said I'm really a newbie to Python but I'm excited about learning it :) How would I go about doing this? Ideally I'd want to be able to say that range 10-20 is equal to "Foo" and have it be the string "Foo" with 7 additional whitespace characters (assuming said field has a length of 10) and have that be a part of the larger 80-character field, but I'm not sure how to do what I'm thinking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您不需要分配给切片,只需使用
% 格式化
。具有 3 个数据项的固定格式的示例:
同样的事情,随数据提供的字段宽度:
分离数据和字段宽度,并使用
zip()
和str.join()技巧:
You don't need to assign to slices, just build the string using
% formatting
.An example with a fixed format for 3 data items:
Same thing, field width supplied with the data:
Separating data and field widths, and using
zip()
andstr.join()
tricks:希望我明白您在寻找什么:某种方法可以通过一个简单的变量方便地识别该行的每个部分,但将其输出填充到正确的宽度?
下面的代码片段可能会为您提供您想要的
结果:
它通过存储一个类级变量
fields
来工作,该变量记录每个字段在输出中出现的顺序以及列数那个字段应该有。__init__
中有相应命名的实例变量,它们最初设置为空字符串。__str__
方法将这些值输出为字符串。 它对类级fields
属性使用列表理解,按名称查找每个字段的实例值,然后根据列左对齐其输出。 然后,生成的字段列表通过空字符串连接在一起。请注意,这不会解析输入,但您可以轻松地重写构造函数以获取字符串并根据
fields
中的字段和字段宽度解析列。 它也不会检查比分配的宽度长的实例值。Hopefully I understand what you're looking for: some way to conveniently identify each part of the line by a simple variable, but output it padded to the correct width?
The snippet below may give you what you want
This yields:
It works by storing a class-level variable,
fields
which records the order in which each field should appear in the output, together with the number of columns that field should have. There are correspondingly-named instance variables in the__init__
that are set to an empty string initially.The
__str__
method outputs these values as a string. It uses a list comprehension over the class-levelfields
attribute, looking up the instance value for each field by name, and then left-justifying it's output according to the columns. The resulting list of fields is then joined together by an empty string.Note this doesn't parse input, though you could easily override the constructor to take a string and parse the columns according to the field and field widths in
fields
. It also doesn't check for instance values that are longer than their allotted width.您可以使用 justify 函数进行左对齐、右对齐和居中给定宽度字段中的字符串。
You can use justify functions to left-justify, right-justify and center a string in a field of given width.
我知道这个线程已经很老了,但是我们使用一个名为 django-copybook 的库。 它与 django 无关(不再)。 我们用它在固定宽度的 cobol 文件和 python 之间切换。 您创建一个类来定义固定宽度记录布局,并且可以在键入的 python 对象和固定宽度文件之间轻松移动:
它还可以处理类似于 Cobol 的 OCCURS 功能的情况,例如当特定部分重复 X 次时
I know this thread is quite old, but we use a library called django-copybook. It has nothing to do with django (anymore). We use it to go between fixed width cobol files and python. You create a class to define your fixed width record layout and can easy move between typed python objects and fixed width files:
It can also handle situations similar to Cobol's OCCURS functionality like when a particular section is repeated X times
我使用了 Jarret Hardie 的例子并稍作修改。 这允许选择文本对齐类型(左对齐、右对齐或居中)。
返回:
I used Jarret Hardie's example and modified it slightly. This allows for selection of type of text alignment(left, right or centered.)
Returns:
解析你的问题有点困难,但我收集到你正在接收一个文件或类似文件的对象,阅读它,并用一些业务逻辑结果替换一些值。 它是否正确?
克服字符串不变性的最简单方法是编写一个新字符串:
话虽如此,听起来对您来说用这个字符串做一些事情很重要,但我不确定那到底是什么。 您是否将其写回输出文件,尝试就地编辑文件,或者其他什么? 我提出这一点是因为创建新字符串(恰好与旧字符串具有相同的变量名)的行为应该强调在转换后执行显式写入操作的必要性。
It's a little difficult to parse your question, but I'm gathering that you are receiving a file or file-like-object, reading it, and replacing some of the values with some business logic results. Is this correct?
The simplest way to overcome string immutability is to write a new string:
Having said that, it sounds like it's important to you that you do something with this string, but I'm not sure exactly what that is. Are you writing it back to an output file, trying to edit a file in place, or something else? I bring this up because the act of creating a new string (which happens to have the same variable name as the old string) should emphasize the necessity of performing an explicit write operation after the transformation.
您可以将字符串转换为列表并进行切片操作。
You can convert the string to a list and do the slice manipulation.
编写“修改”字符串的函数很容易。
用法:
It is easy to write function to "modify" string.
Usage: