【问题标题】:How do i convert entries of text file to .csv file using conditions [closed]如何使用条件将文本文件条目转换为 .csv 文件 [关闭]
【发布时间】:2020-03-24 03:39:29
【问题描述】:

我有 .txt 文件,其中包含以下几个条目

.I 3192
.T
The Lincoln Keyboard - a Typewriter Keyboard Designed 
for Computers Input Flexibility
.W
   A new typewriter keyboard, for direct and punched paper tape computer input
will replace the usual commercial keyboard with 88 characters chosen for the 
convenience  of programmers.
.B
CACM July, 1958
.A
Vanderburgh, A.
.N
CA580702 ES March 17, 1982 10:10 AM
.X
1083    5   3192
3192    5   3192
3192    5   3192

该文件一个接一个地包含多个条目。 .I.T.W.B.A.N.X都是条目中的字段。每个新条目都以.I 开头,并一直持续到另一个条目开始。有些条目没有一个或多个字段,换句话说完全跳过了一个或多个字段。

我正在尝试将整个文本文件转换为 csv 文件,其中每一行以下列方式代表一个条目

content of .Icontent of .Tcontent of .Wcontent of .Bcontent of .Acontent of .Ncontent of .X

如果任何条目没有该字段,则 csv 文件中该字段的位置应为空值。

【问题讨论】:

  • “我正在尝试”听起来您已经尝试过。你能分享你的方法并强调缺少什么吗?

标签: python-3.x csv text text-processing


【解决方案1】:

使用适当的正则表达式并拆分您的文本。可能的解决方案:

parts = [match for match in re.split(r'\.[A-Z]\s+', input_text) if len(match) > 0]

【讨论】:

    猜你喜欢
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多