如何拆分字符串解析并结构化

【问题】

I have the following code, however there seems to be an error within it somewhere. I get output (a) but require output (b) - see below. Can anyone see where I am going wrong? All files are tab-delimited.

Code:

import sys

outfile_name = sys.argv[-1]

filename1 = sys.argv[-2]

filename2 = sys.argv[-3]

fileIn1 = open(filename1, "r")

fileIn2 = open(filename2, "r")

fileOut = open(outfile_name, "w")

dict = {}

a = open(filename1)

b = open(filename2)

for line in a:

words = line.split("\t")

if len(words) != 1:

    target = words[0]

    for word in words[1:]:

        dict[word] = target

for line in b:

words = line.split("\t")

if words[0] in dict.keys() and words[1] in dict.keys():

        fileOut.write(dict[words[0]] + "\t" + dict[words[1]] + "\n")

elif words[0] in dict.keys() and words[1] not in dict.keys():

        fileOut.write(dict[words[0]] + "\t" + words[1] + "\n")

elif words[0] not in dict.keys() and words[1] in dict.keys():

        fileOut.write(words[0] + "\t" + dict[words[1]] + "\n")

elif words[0] not in dict.keys() and words[1] not in dict.keys():

        fileOut.write(words[0] + "\t" + words[1] + "\n")

fileOut.close()

filename1:

Area_1 Area_2

A   B

A   C

A   D

D   B

D   C

L   B

L   C

L   A

D   L

K   A

K   B

K   C

K   D

K   L

D   P

D   R

L   P

L   R

K   P

K   R

A   H

D   H

L   H

K   H

B   P

B   R

R   P

A   I

D   I

I   L

I   K

C   H

I   H

C   H

J   K

J   X

J   Y

J   Z

K   X

K   Y

Y   Z

K   Z

X   Y

X   Z

M   G

N   T

O   S

S   Q

filename2:

Incident_00000001       A       D       L       K

Incident_00000002       B       P       R

Incident_00000003       C       F       W

Incident_00000004       J       I

M

N

O

Incident_00000005       Q       S

X

Y

Z

G

T

output (b) - undesired output that I am getting:

Area_1  Area_2

Incident_00000001   B

Incident_00000001   C

Incident_00000001   D

Incident_00000001   B

Incident_00000001   C

Incident_00000001   B

Incident_00000001   C

Incident_00000001   A

Incident_00000001   L

K   A

K   B

K   C

K   D

K   L

Incident_00000001   P

Incident_00000001   Incident_00000002

Incident_00000001   P

Incident_00000001   Incident_00000002

K   P

K   Incident_00000002

Incident_00000001   H

Incident_00000001   H

Incident_00000001   H

K   H

Incident_00000002   P

Incident_00000002   Incident_00000002

R   P

Incident_00000001   Incident_00000003

Incident_00000001   Incident_00000003

I   L

I   Incident_00000004

Incident_00000003   H

I   H

Incident_00000003   H

Incident_00000004   Incident_00000004

Incident_00000004   X

Incident_00000004   Y

Incident_00000004   Z

K   X

K   Y

Y   Z

K   Z

X   Y

X   Z

M   G

N   T

O   S

Incident_00000005   Incident_00000005

What I am looking to get (output (c)) is:

Area_1  Area_2

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000003

Incident_00000001   Incident_00000001

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000003

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000003

Incident_00000001   Incident_00000001

Incident_00000001   Incident_00000001

Incident_00000001   Incident_00000001

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000003

Incident_00000001   Incident_00000001

Incident_00000001   Incident_00000001

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000002

Incident_00000001   Incident_00000002

Incident_00000001   H

Incident_00000001   H

Incident_00000001   H

Incident_00000001   H

Incident_00000002   Incident_00000002

Incident_00000002   Incident_00000002

Incident_00000002   Incident_00000002

Incident_00000001   Incident_00000004

Incident_00000001   Incident_00000004

Incident_00000004   Incident_00000001

Incident_00000004   Incident_00000001

Incident_00000003   H

Incident_00000004   H

Incident_00000003   H

Incident_00000004   Incident_00000001

Incident_00000004   X

Incident_00000004   Y

Incident_00000004   Z

Incident_00000001   X

Incident_00000001   Y

Y   Z

Incident_00000001   Z

X   Y

X   Z

M   G

N   T

O   Incident_00000005

Incident_00000005   Incident_00000005

【回答】

可将file2过滤再整理为便于查询的二维表,字段incident用于显示值,字段code是集合,用于在file1中进行查询。之后就是简单的编码反显:如果file1中的编码属于file2某条记录的code集合,则输出该条记录的incident,否则输出原编码。

上述算法涉及结构化计算、集合运算、有序拆分字符串,逻辑虽然比较简单,但python要从底层实现,代码比较难写。如无特殊要求可用SPL实现,代码简单易懂: 

A
1 =file("d:/file1.txt").import@t()
2 =file("d:/file2.txt").read@n()
3 =A2.select(pos(~,"Incident"))
4 =A3.new((t=~.split("\t"))(1):incident,t.to(2,):code)
5 =A1.new(ifn(A4.select@1(code.pos(A1.Area_1)).incident,Area_1):Area_1,ifn(A4.select@1(code.pos(A1.Area_2)).incident,Area_2):Area_2)

A1:读取file1文本

A2:按行读取file2文本

A3:从A2中选出含"Incident"子串的行

A4:将A3整理为incident, code两个字段组成的序表

通过~.split("\t")把A3的每一行按照分隔符拆成序列,其中incident字段值为该序列第一个成员,code字段值为剩下的成员组成的序列。

A5:根据序表A1生成Area_1和Area_2组成的新序表,如果file1中的编码属于file2某条记录的code集合,新序表的字段值为该条记录的incident值,否则为原编码。

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习编程?其实不难,不过在学习编程之前你得先了解你的目的是什么?这个很重要,因为目的决定你的发展方向、决定你的发展速度。
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面设计类、前端与移动、开发与测试、营销推广类、数据运营类、运营维护类、游戏相关类等,根据不同的分类下面有细分了不同的岗位。
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生学习Java开发,但要结合自身的情况,先了解自己适不适合去学习Java,不要盲目的选择不适合自己的Java培训班进行学习。只要肯下功夫钻研,多看、多想、多练
Can’t connect to local MySQL server through socket \'/var/lib/mysql/mysql.sock问题 1.进入mysql路径
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 sqlplus / as sysdba 2.普通用户登录
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服务器有时候会断掉,所以写个shell脚本每五分钟去判断是否连接,于是就有下面的shell脚本。
BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。
假如你已经使用过苹果开发者中心上架app,你肯定知道在苹果开发者中心的web界面,无法直接提交ipa文件,而是需要使用第三方工具,将ipa文件上传到构建版本,开...
下面的 SQL 语句指定了两个别名,一个是 name 列的别名,一个是 country 列的别名。**提示:**如果列名称包含空格,要求使用双引号或方括号:
在使用H5混合开发的app打包后,需要将ipa文件上传到appstore进行发布,就需要去苹果开发者中心进行发布。​
+----+--------------+---------------------------+-------+---------+
数组的声明并不是声明一个个单独的变量,比如 number0、number1、...、number99,而是声明一个数组变量,比如 numbers,然后使用 nu...
第一步:到appuploader官网下载辅助工具和iCloud驱动,使用前面创建的AppID登录。
如需删除表中的列,请使用下面的语法(请注意,某些数据库系统不允许这种在数据库表中删除列的方式):
前不久在制作win11pe,制作了一版,1.26GB,太大了,不满意,想再裁剪下,发现这次dism mount正常,commit或discard巨慢,以前都很快...
赛门铁克各个版本概览:https://knowledge.broadcom.com/external/article?legacyId=tech163829
实测Python 3.6.6用pip 21.3.1,再高就报错了,Python 3.10.7用pip 22.3.1是可以的
Broadcom Corporation (博通公司,股票代号AVGO)是全球领先的有线和无线通信半导体公司。其产品实现向家庭、 办公室和移动环境以及在这些环境...
发现个问题,server2016上安装了c4d这些版本,低版本的正常显示窗格,但红色圈出的高版本c4d打开后不显示窗格,
TAT:https://cloud.tencent.com/document/product/1340