Skip to main content
deleted 80 characters in body
Source Link
chw21
  • 8.1k
  • 1
  • 17
  • 33

I have written a python program that iterates over the file only once, reads all the important data into a dict, and then writes the dict into the output file.

#!/usr/bin/env python3
import collections

output = collections.OrderedDict()

defwith my_splitopen(lineinputfile, 'r') as infile:
    for line in infile:
        dat, tmp = line.strip().split(',')
        if '_''_val' in tmp:
        var    key, strvalidxstr = tmp.split('_val')
        val = int(strval)
    else:
        varidx = tmpint(idxstr)
        val = 0else:
    return dat, var, val

with open('infile.txt', 'r') as infile:
  key = fortmp
 line in infile:
        dat, var, validx = my_split(line.strip())0
        output.setdefault(varkey, ["", "", ""])[val][idx] = dat

with open('outfile.txt'outoutfile, 'w') as outfile:
    for var in output:
        v = output[var]
        outfile.write('{} {}\n'.format(var, ' '.join(v)))

Update: modified according to comments

I have written a python program that iterates over the file only once, reads all the important data into a dict, and then writes the dict into the output file.

#!/usr/bin/env python3
import collections

output = collections.OrderedDict()

def my_split(line):
    dat, tmp = line.split(',')
    if '_' in tmp:
        var, strval = tmp.split('_val')
        val = int(strval)
    else:
        var = tmp
        val = 0
    return dat, var, val

with open('infile.txt', 'r') as infile:
    for line in infile:
        dat, var, val = my_split(line.strip())
        output.setdefault(var, ["", "", ""])[val] = dat

with open('outfile.txt', 'w') as outfile:
    for var in output:
        v = output[var]
        outfile.write('{} {}\n'.format(var, ' '.join(v)))

Update: modified according to comments

I have written a python program that iterates over the file only once, reads all the important data into a dict, and then writes the dict into the output file.

#!/usr/bin/env python3
import collections

output = collections.OrderedDict()

with open(inputfile, 'r') as infile:
    for line in infile:
        dat, tmp = line.strip().split(',')
        if '_val' in tmp:
            key, idxstr = tmp.split('_val')
            idx = int(idxstr)
        else:
            key = tmp
            idx = 0
        output.setdefault(key, ["", "", ""])[idx] = dat

with open(outoutfile, 'w') as outfile:
    for var in output:
        v = output[var]
        outfile.write('{} {}\n'.format(var, ' '.join(v)))

Update: modified according to comments

added 29 characters in body
Source Link
chw21
  • 8.1k
  • 1
  • 17
  • 33

I have written a python program that iterates over the file only once, reads all the important data into a dict, and then writes the dict into the output file.

#!/usr/bin/env python3
import collections

output = collections.OrderedDict()

def my_split(line):
    dat, tmp = line.split(',')
    tryif '_' in tmp:
        var, strval = tmp.split('_val')
        val = int(strval)
    exceptelse:
        var = tmp
        val = 0
    return dat, var, val

with open(inputfile'infile.txt', 'r') as infile:
    for line in infile:
        dat, var, val = my_split(line.strip())
        if var not in output:
            output[var] =.setdefault(var, ["", "", ""]
        output[var][val])[val] = dat

with open(outputfile'outfile.txt', 'w') as outfile:
    for var in output:
        v = output[var]
        outfile.write(var + ' '{} +{}\n'.format(var, ' '.join(output[var]v)+'\n'))

Update: modified according to comments

I have written a python program that iterates over the file only once, reads all the important data into a dict, and then writes the dict into the output file.

#!/usr/bin/env python3
import collections

output = collections.OrderedDict()

def my_split(line):
    dat, tmp = line.split(',')
    try:
        var, strval = tmp.split('_val')
        val = int(strval)
    except:
        var = tmp
        val = 0
    return dat, var, val

with open(inputfile, 'r') as infile:
    for line in infile:
        dat, var, val = my_split(line.strip())
        if var not in output:
            output[var] = ["", "", ""]
        output[var][val] = dat

with open(outputfile, 'w') as outfile:
    for var in output:
        outfile.write(var + ' ' + ' '.join(output[var])+'\n')

I have written a python program that iterates over the file only once, reads all the important data into a dict, and then writes the dict into the output file.

#!/usr/bin/env python3
import collections

output = collections.OrderedDict()

def my_split(line):
    dat, tmp = line.split(',')
    if '_' in tmp:
        var, strval = tmp.split('_val')
        val = int(strval)
    else:
        var = tmp
        val = 0
    return dat, var, val

with open('infile.txt', 'r') as infile:
    for line in infile:
        dat, var, val = my_split(line.strip())
        output.setdefault(var, ["", "", ""])[val] = dat

with open('outfile.txt', 'w') as outfile:
    for var in output:
        v = output[var]
        outfile.write('{} {}\n'.format(var, ' '.join(v)))

Update: modified according to comments

Source Link
chw21
  • 8.1k
  • 1
  • 17
  • 33

I have written a python program that iterates over the file only once, reads all the important data into a dict, and then writes the dict into the output file.

#!/usr/bin/env python3
import collections

output = collections.OrderedDict()

def my_split(line):
    dat, tmp = line.split(',')
    try:
        var, strval = tmp.split('_val')
        val = int(strval)
    except:
        var = tmp
        val = 0
    return dat, var, val

with open(inputfile, 'r') as infile:
    for line in infile:
        dat, var, val = my_split(line.strip())
        if var not in output:
            output[var] = ["", "", ""]
        output[var][val] = dat

with open(outputfile, 'w') as outfile:
    for var in output:
        outfile.write(var + ' ' + ' '.join(output[var])+'\n')