Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从结构化数据复制var中的值_Python_Short - Fatal编程技术网

Python 从结构化数据复制var中的值

Python 从结构化数据复制var中的值,python,short,Python,Short,我在“bulk_data”var中有一个bulk数据,现在需要在sub var中查找并复制它,如下所示,如何使用python进行操作 bulk_data = """F0142514RM/JRSE1420 Mod/4758 F0144758RM/JRSE935 Mod/23 F014GS4RM/JRSE10 Mod/445 """ typeA1 = <start with RM/>"JRSE1420"<until space> in 1st line typeA2 = &

我在“bulk_data”var中有一个bulk数据,现在需要在sub var中查找并复制它,如下所示,如何使用python进行操作

bulk_data = """F0142514RM/JRSE1420 Mod/4758
F0144758RM/JRSE935 Mod/23
F014GS4RM/JRSE10 Mod/445
"""

typeA1 = <start with RM/>"JRSE1420"<until space> in 1st line
typeA2 = <start with RM/>"JRSE935"<until space> in 2nd line
typeA3 = <start with RM/>"JRSE10"<until space> in 3rd line

typeB1 = <start with space after typeA1>"Mod/4758"<until end of the line> in 1rd line
typeB2 = <start with space after typeA2>"Mod/23"<until end of the line> in 2nd line
typeB3 = <start with space after typeA3>"Mod/445"<until end of the line> in 3rd line


Overall result would be:
typeA1 = 'JRSE1420'
typeA2 = 'JRSE935'
typeA3 = 'JRSE10'

typeB1 = 'Mod/4758'
typeB2 = 'Mod/23'
typeB3 = 'Mod/445'
bulk_data=“”F0142514RM/JRSE1420 Mod/4758
F0144758RM/JRSE935 Mod/23
F014GS4RM/JRSE10 Mod/445
"""
第1行中键入A1=“JRSE1420”
第2行中键入A2=“JRSE935”
第三行中键入A3=“JRSE10”
在1rd行中键入B1=“Mod/4758”
第二行中的typeB2=“Mod/23”
第三行中键入B3=“Mod/445”
总体结果将是:
typeA1='JRSE1420'
typeA2='JRSE935'
typeA3='JRSE10'
类型B1=‘Mod/4758’
typeB2=‘Mod/23’
类型B3='Mod/445'
是否有任何关于此类数据处理的学习手册?

您可以使用该模块

count = 1
li = []
with open('data') as f:
    for line in f:
        line = line.split()
        if line:
            a, b = line
            a = a[a.index('/')+1:]
            li.append("TypeA{} = {} ".format(count, a))
            li.append("TypeB{} = {} ".format(count, b))
            count += 1

for el in sorted(li):
    print(el)

TypeA1 = JRSE1420 
TypeA2 = JRSE935 
TypeA3 = JRSE10 
TypeB1 = Mod/4758 
TypeB2 = Mod/23 
TypeB3 = Mod/445

为什么是你?看起来所有的东西都已经被不同的字符正确地分开了

lines = bulk_data.splitlines()
typeA1_, typeB1 = lines[0].split(' ')
typeA1 = typeA1_.split('/')[1]

嗯?“短拷贝”?我很难理解这里的问题。然而,也许你应该看看正则表达式(module
re
),这意味着find和copy肯定会使用正则表达式==============不工作==========a=“”F0142514RM/JRSE1420 Mod/4758 F0144758RM/JRSE935 Mod/23 F014GS4RM/JRSE10 Mod/445”“”为a:mo中的行导入re=re.search(r'(^\w+\s*=).*([^”]*”,如果mo:print(mo.group(1),mo.group(2))=========@我已使用以下行作为您的数据。我的错误。我将删除此内容并发布新内容。太好了,我正在等待:)
lines = bulk_data.splitlines()
typeA1_, typeB1 = lines[0].split(' ')
typeA1 = typeA1_.split('/')[1]