0

I have a tab-delimeted file that contains 3 columns. I would like to add a new first column of just same number 1.

inputfile is

a 3 6
b 3 5
c 3 5 
d 8 4 

This is what I would like to have for my outputfile:

1 a 3 6
1 b 3 5
1 c 3 5
1 d 8 4

This is what I have so far:

#!/usr/bin/env python
import sys
import csv  
f=open('inputfile.txt', 'r+')
t=[]
for line in f.readlines():
    t.append('\n')
    t.append(1)
    f.writelines(t)

However, I am getting an error: Traceback (most recent call last): File "./py.py", line 6, in sys.stdout('inputfile.txt', 'w') TypeError: 'file' object is not callable

3
  • 5
    sys.stdout is used for output stream. It's not a function. Commented Apr 1, 2016 at 18:33
  • 1
    You'd want to open your output file the same way you opened your input. fout = open('outputfile.txt', 'w') and fin=open('inputfile.txt', 'r+') Also, why are you trying to read and write from your input file? Commented Apr 1, 2016 at 18:37
  • File objects in python used by the interpreter for representing the open files. And sys.stdout object initialized to file objects corresponding to the interpreter’s standard output. It's just a regular text file like those returned by the open() function. Read more docs.python.org/3.4/library/sys.html#sys.stdout Commented Apr 1, 2016 at 18:41

4 Answers 4

2

Simply open both files and write to it, concatenating a 1 and tab \t to each line in a running list. Then output list to new file:

f1 = "Input.txt"
f2 = "Output.txt"

t = []
with open(f1, 'r') as txt1:
    for rline in txt1:       
        t.append("1\t" + rline)

with open(f2, 'w') as txt2:
    for i in t:
        txt2.write(i)

#1  a   3   6
#1  b   3   5
#1  c   3   5
#1  d   8   4

Alternatively, to avoid use of a list (but requires appending to file with 'a'):

with open(f1, 'r') as txt1:
    for rline in txt1:       
        rline = "1\t" + rline

        with open(f2, 'a') as txt2:
            txt2.write(rline)

And even further suggested by @JonClements that avoids the overhead of opening/closing file with each line:

with open(f1) as txt1, open(f2, 'w') as txt2:
        txt2.writelines('1\t' + line for line in txt1) 
Sign up to request clarification or add additional context in comments.

2 Comments

You can change your later example to be: with open(f1) as txt1, open(f2, 'w') as txt2 then make your entire statement block txt2.writelines('1\t' + line for line in txt1) - this'll avoid the overhead of opening/closing the file each line of the input...
Just to note - it's not a list-comp - it's a generator expression :)
1

You don't need sys module for the solution:

#!/usr/bin/env python
output_file = open('output.txt', 'w')
input_file = open('input.txt', 'r+')
for line in input_file.readlines():
    line = line.split(" ")
    line.insert(1, str(1))
    line = (" ").join(line)
    output_file.write(line)

input_file.close()    
output_file.clsoe()

cat output.txt gives output:

a 1 3 6
b 1 3 5
c 1 3 5
d 1 8 4

Comments

0

This will give you exactly what your desired output shows:

fin = open('inputfile.txt', 'r+')
fout = open('outputfile.txt', 'w')
t=[]

for line in fin.readlines():
    t.append('1 ' + line)

fout.writelines(t)

1 Comment

OP states I have a tab-delimeted file (so your '1 ' + line' will be prepending a value to the first column - not creating a new one) - also - readlines will read the entire file into a list first, you're then append then to another completely new list - you can just loop over fin directly and write line-by-line.
0

If you do not want nothing else you can skip csv module and just prepend each line in a file, like this:

open('out.txt', 'w').writelines(["1\t" + line for line in open("in.txt")])

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.