-1

I have a program in python that produce some text plus one number for each line. I want to save it to a file. here example of output after saving:

some text            6.2
some other text            7.6
some other text here too            6.8
some text            8

as you can see the numbers is not in one columns. I used some spaces between texts and numbers but I want put all numbers in one column. how can I do that? with file write method or any other thing?

Edit:

I want to place those numbers in one column to save in a file and with write method. it's very easy if I could use print function to display data in monitor but I want to save in a file. other similar posts show how to display in output not save in a file.

import json
import urllib.request as rq

def func():

    data=rq.urlopen('http://yts.to/api/v2/list_movies.json?quality=3d&limit=50')
    dataj=json.loads(data.read().decode())

    with open('yts.txt','w+') as f:
        f.write("Title                       Imdb rating\n")
        f.write("---------------------------------------\n")

        for i in range(len(dataj["data"]["movies"])):
            f.write(dataj["data"]["movies"][i]["title"]+'\t'+str(dataj["data"]["movies"][i]["rating"]))
            f.write("\n")


if __name__=="__main__":
    func()
Sara Santana
  • 1,001
  • 1
  • 11
  • 22
  • Add the program to the question it will help us debug. Aside: save your file as a tsv, It will show the columns perfectly – Bhargav Rao Mar 30 '15 at 16:40
  • You make it sound like you intentionally used spaces as a delimiter. Are they regular? and is it not feasible to use a different delimiter which is more standardised? (pipes, tabs, commas, semi-colons) – NDevox Mar 30 '15 at 16:42
  • The title and the content of the question are contradictory: one line or two columns on many lines? – A.L Mar 30 '15 at 16:54
  • Agreed with Tui, these solutions are far better than my answer. thanks for posting. – tnknepp Mar 30 '15 at 19:50

4 Answers4

1

If the labels are defined at runtime it might take some figuring on your side, or a pretty printer I haven't looked for because this doesn't come up often for me. In general, if you use tabs instead of spaces though it will help get things lined up, though you have to make sure everything would be tabbed out past the end of the longest label.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
1

Try putting one or multiple \t (tabs) between text and numbers instead of spaces. Example: some text\t6.2.

Timur Osadchiy
  • 5,699
  • 2
  • 26
  • 28
  • not helping because length of texts is not same so in each line numbers print in different locations. – Sara Santana Mar 30 '15 at 16:47
  • 1
    that should just be because you're looking at it in a text editor. but if you were to save it as .csv and look at it in something like excel that can interpret delimitters, they would all be in the same column – xgord Mar 30 '15 at 16:56
1

I don't know much about your program from what you've given. But check this: https://pypi.python.org/pypi/tabulate

If this is a homework assignment and you cannot use external libraries, you could define your "table" cells to be a certain width. Find the string length and subtract that from the cell width to get the number of empty spaces to fill (you could also then add ellipsis if that is acceptable and the cell cannot fit your string).

If you're trying to pass information between programs though, you should always have the data in a fixed form (delimiters etc). "Pretty printing" is only for humans.

Guru Evi
  • 184
  • 5
1

I don't understand the purpose of strict "pretty" formatting, but when in need...

If you have a list of strings and associated numbers (I'll just make up data in a dictionary, but the same general idea should hold for your application):

a = {'aaaaa':'8', 'bb':'1', 'ccccccccccccccccccccccc':'512', 'd':'9999', 'eeee':'0'}

maxwidth = max( [len(r) for r in a] )

for r in a:
    print(r + a[r].rjust(20 + maxwidth - len(r)))

aaaaa                                     8
eeee                                      0
ccccccccccccccccccccccc                 512
bb                                        1
d                                      9999
tnknepp
  • 5,888
  • 6
  • 43
  • 57