Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a number to a column with python

Tags:

python

I am very new to python and I would be grateful for some guidance with the following. I have a text file with over 5 million rows and 8 columns, I am trying to add "15" to each value in column 4 only.

For example:

  10  21  34  12  50  111  234  21  7
  21  10  23  56  80   90  221  78 90

Would be changed to:

  10  21  34  12  **65**  111  234  21  7
  21  10  23  56  **95**   90  221  78 90

My script below allows me to isolate the column, but when I try to add any amount to it i return "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'"

file = open("file.txt")
column = []

for line in file:
    column.append(int(line.split("\t")[3]))

print column

Any advice would be great.

like image 494
sheaph Avatar asked Sep 05 '25 10:09

sheaph


1 Answers

try this to get you started -- there are many better ways using libraries but this will show you some better file handling basic methods anyway. works for the data you posted -- as long as the delimiter in your files is double space (" ") and that everything can be cast to an int. If not.....

Also -- note the correct way to start a script is with:

if __name__ == "__main__":

this is because you wont generally want any code to execute if you are making a library...

__author__ = 'charlie'

in_filename = "in_file.txt"
out_filename = "out_file.txt"
delimiter = "  "

def main():

    with open(in_filename, "r") as infile:
        with open(out_filename, "w") as outfile:
            for line in infile:

                ldata = line.split(delimiter)

                ldata[4] = str(int(ldata[4]) + 15)

                outfile.write(delimiter.join(ldata))


if __name__ == "__main__":
    main()
like image 175
Transact Charlie Avatar answered Sep 09 '25 02:09

Transact Charlie