user2355306
user2355306

Reputation: 387

add new digits to a column of a text file in Python

I am trying to edit some text files I have in order to add value to one of the columns. I would like to add two new digit to the second column of my files where are separated with space. The first column would ended on the 13 character, then there is two space and then add the new two digits and the other columns would remain without change.

I have written the following script but unfortunately it does work. I have be thankful if somebody could help me to find my mistake.

%********function************
def add_num(infile,outfile):
    output = ["%s  %s%s" %(item.strip()[:13] ,32,item.strip()[16:]) for item in infile]
    outfile.write("\n".join(output))
    outfile.close()
    return outfile
%*********************************
%**********main code for calling the function*******
import os, Add32
folder = 'E:/MLS_HFT/TEST/Stuttgart_2009_pointclouds/'
for filename in os.listdir(folder):
      infilename = os.path.join(folder,filename)
      if not os.path.isfile(infilename): continue
      base,extension = os.path.splitext(filename)
      infile= open(infilename, 'r')
      outfile = open(os.path.join(folder, '{}_32{}'.format(base,extension)),'w')
      add32.add_num(infile,outfile)

and this is a sample of a data:

 399299.855212  512682.330  5403021.950  303.471  64    1  1  2        75
 399299.855212  512681.470  5403020.790  302.685  1     1  2  2        75
 399299.855222  512682.360  5403021.970  303.526  79    1  1  2        76

Upvotes: 1

Views: 160

Answers (4)

falsetru
falsetru

Reputation: 369134

with open('infile.txt', 'rb') as infile, open('outfile.txt', 'wb') as outfile:
    outfile.writelines(line[:15] + '32' + line[15:] for line in infile)

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

Use str.split:

col = 2
#just pass filenames to your function and use `with` statement for handling files.
with open(infile) as f, open(outfile, 'w') as out:
    for line in f:
        spl = line.split(None, col)
        spl[col -1] = '32' + spl[col -1]
        out.write(" ".join(spl))
...         
399299.855212 32512682.330 5403021.950  303.471  64    1  1  2        75

399299.855212 32512681.470 5403020.790  302.685  1     1  2  2        75

399299.855222 32512682.360 5403021.970  303.526  79    1  1  2        76

Working version of your code:

def add_num(infile,outfile):
   with open(infile) as f, open(outfile, 'w') as out:
      output = ["%s  %s%s\n" %(item.strip()[:13] ,32,item.strip()[16:]) for item in f]
      out.writelines(output)

outfile = os.path.join(folder, '{}_32{}'.format(base,extension))
add_num(infilename,outfile)

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 113988

def add_num(infile,outfile):
    output = ["%s  %s%s" %(item.strip()[:13] ,32,item.strip()[16:]) for item in infile]
    outfile.write("\n".join(output))
    outfile.close()
    return outfile

add_num(open("infile.data"),open("outfile.data","w"))

then look at outfile.data ... nothing wrong with your function probably how you are calling it

Upvotes: 2

Stephan
Stephan

Reputation: 17981

You can use slice notation to insert characters, for example

myString = myString[:15] + "  12" + myString[15:]

will insert 4 characters at position 15

the first slice myString[:15] will get a substring from the beginning to the 15th position

the second slice myString[15:] will get a substring from the 15th position to the end

you can concatenate that by adding in your characters to the middle

Upvotes: 0

Related Questions