4

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.

5
  • Are you sure these lines are tab delimited? Also you probably want to do rstrip() on the line to get rid of the newline at the end. Commented Jun 17, 2014 at 14:21
  • Yes these lines are tab delimited, what i've included is representative of the data. Commented Jun 17, 2014 at 14:24
  • This works fine for me, are you sure your file.txt is formatted correctly? Commented Jun 17, 2014 at 14:28
  • Please do a print line.split("\t")[3] before adding something to it. Also, please show the line where you're getting the error. Finally, why [3] if you want to alter the fifth column? Commented Jun 17, 2014 at 14:31
  • 1
    Side Note: If it is computation heavy, I would look into Pandas! Commented Jun 17, 2014 at 14:34

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Read the file in with csvparser with the delimiter set to \t
As @Shaun said - you could use csvparser or some other library to do it also. I think you are looking for a 'raw' python example though.
1

With Pandas :

import pandas as pd

df = pd.read_clipboard(header=None)
df[4] += 15

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.