0

I am trying to read a txt-file with many values of pairs in two colmns (X-Y values) and I wand them to be writen in another txt-file with 4 pairs X-Y at each row (8 values per row).

So, from that:

0,038043    0,74061
0,038045    0,73962
0,038047    0,73865
0,038048    0,73768
0,03805 0,73672
0,038052    0,73577
0,038053    0,73482
0,038055    0,73388
0,038057    0,73295
0,038058    0,73203
0,03806 0,73112
0,038062    0,73021
0,038064    0,72931
0,038065    0,72842
0,038067    0,72754
0,038069    0,72666

to that:

    0,038043    0,74061 0,038045    0,73962 0,038047    0,73865 0,038048    0,73768 
0,03805 0,73672 0,038052    0,73577 0,038053    0,73482 0,038055    0,73388
    0,038057    0,73295 0,038058    0,73203 0,03806 0,73112 0,038062    0,73021
    0,038064    0,72931 0,038065    0,72842 0,038067    0,72754 0,038069    0,72666

I tried:

import itertools
files = [1, 2 ,3 ,4, 5, 6, 7,
8, 9, 10, 11, 12, 13]
for k in files:
    print 'k =',k
    with open("deflag_G{k}.inp".format(k=k)) as f1:
       with open("deflag_G_{k}.inp".format(k=k),"w") as f2:
            f2.writelines(itertools.islice(f1, 4, None))
    f2.close()
    f1.close()

But I am not taking 4 pairs (8 values per line) in the new file. Any help is appreciated.

GeMa
  • 149
  • 1
  • 3
  • 13

3 Answers3

0

One way to approach this is to spit the logic out. First get the data to a list of X-Y, then chunk the data to rows of 8 X-Y and then save the data (ie write data to another text file)

The chunk method I've borrowed from another stack overflow answer.

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n] 

input = [
'0,038043, 0,74061',
'0,038045, 0,73962',
'0,038047, 0,73865',
'0,038048, 0,73768',
'0,03805,  0,73672',
'0,038052, 0,73577',
'0,038053, 0,73482',
'0,038055, 0,73388',
'0,038057,  0,73295',
'0,038058, 0,73203',
'0,03806, 0,73112',
'0,038062, 0,73021',
'0,038064, 0,72931',
'0,038065, 0,72842',
'0,038067, 0,72754',
'0,038069, 0,7266'
] # Convert data to list of X-Y    

for x in list(chunks(input, 8)): # 8 is the number of chunk
    print(x) # This contains an array of 8 X-Y (e.g ['0,038043, 0,74061', '0,038045, 0,73962', '0,038047, 0,73865', '0,038048, 0,73768', '0,03805, 0,73672', '0,038052, 0,73577', '0,038053, 0,73482', '0,038055, 0,73388'])

... you could add your logic to save data to csv.
Greg
  • 4,468
  • 3
  • 16
  • 26
0

If you have a generic batcher that will work on an iterable, you can use it directly on the file object to read lists of up to 4 lines. For example:

def batcher(iterable, n):
    while True:
        vals = []
        for i in range(n):
            try:
                vals.append(next(iterable))
            except StopIteration:
                if vals:
                    yield vals
                return
        yield vals

with open("input.txt") as f1:
    with open("output.txt", "w") as f2:
        for lines in batcher(f1, 4):
            f2.write(' '.join((l.replace("\n", "") for l in lines)) + '\n')
alani
  • 12,573
  • 2
  • 13
  • 23
  • @GeMa I don't know about the speed, but it will not read them all into memory at the same time. – alani Jun 05 '20 at 12:27
0
import itertools
#i = 0
j = 0
for k in range (1,14):
    print 'k =',k
    with open("deflag_G{k}.inp".format(k=k)) as f1:
       with open("deflag_G_{k}.inp".format(k=k),"w") as f2:
            #lines = f1.readlines()
            for i, line in enumerate(f1):
                print i 
                j = j + 1
                print j
                b = line.split()[0]
                s = float(b)
                #print "b:", b
                d = line.split()[1]
                e = float(d)                
                if j == 8:
                   j = 0   
# ('%s'%s + ' , ' + '%e'%e + '\n')                   
                   f2.write(line.rstrip('\n') + ","+ '\n')
                else:                  
                    print(repr(line))               
            #if line.startswith(searchquery):
                    f2.write(line.rstrip('\n') + ",  " )
            #f2.write('%s'%listc + "\n")
               # i = i + 1
        #else :
        #    i = i+1
        #os.close(f1)
    f1.close()
    f2.close()
GeMa
  • 149
  • 1
  • 3
  • 13