0

I am a newbie to Python(2.7.15). I am trying to append the output of my Python script to a text file separated by a tab.

Below is part of my code:

for key,value in d.iteritems():
    with open('output.txt', 'a') as file:
        file.write(key + "\t" + value + "\t")
.....
....
 for item in data["response"]["docs"]:
        titleValue = (item['title'])
 with open('/tmp/output.txt', 'a') as file:
        file.write(titleValue + "\n")

My output file looks like below:

FirstKey    FirstValue
    FirstTitle
SecondKey    SecondValue
    SecondTitle

Basically I want to print the values as tab separated value in the same line and print the next line as tab separated values in a new line and so on as below:

FirstKey    FirstValue    FirstTitle
SecondKey   SecondValue   SecondTitle

How can I do that? file.write is printing it in a new line. I want it to print in the same line

3
  • Don't have time (or Python 2) to test a full answer, sorry. However, I think you need to do this within a single with open.... Also, research for loops to see how to iterate over multiple controls simultaneously. Commented Feb 22, 2019 at 22:31
  • The thing is I cannot do the file write in a single open. I have to grab the output from two different loops and then write it respectively. Commented Feb 22, 2019 at 22:36
  • Looking more closely, I don't see how the code you show produces the output you present. I'd think 'SecondKey SecondValue' would appear before 'FirstTitle'. Commented Feb 22, 2019 at 22:44

2 Answers 2

1

First it should be noted that file.write() does not add any newline characters implicitly, and those line feeds you get are passed somehow within the argument supplied to that function.

Unfortunately it is not clear from the example how the written keys and values are obtained. However note that the title lines (for example, the one with FirstTitle) contain an indent before the title itself, probably a tab character. From that I would suppose that the values in d dictionary are some strings that end with a newline character.

That way key + "\t" + value + "\t" (written to the file) would result in something like "FirstKey\tFirstValue\n\t" which explains the output you get. In order to get the expected results ensure that those values do not contain trailing newline characters, for example, by calling str.rstrip for them:

value = value.rstrip()

Sign up to request clarification or add additional context in comments.

Comments

1

It's more pythonic to use %, format and join than +.

Firstly, join run more quickly, specially when the number of text is large, and your code is more clear. as follows:

words = ["Thanks", "for", "contributing", "an", "answer", "to", "Stack", "Overflow!"]
text = "\t".join(words)

Secondly, format can format your text as you want. as follows:

text1 = "{}\t{}\t{}\t".format(1, 'A', 1.343)  # output: 1   A   1.343
text2 = "%04d\t%5f" % (1, 3.1415926)   # output: 0001   3.141593

1 Comment

Thanks. This is helpful

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.