-1

I have a function that will will give me the outermost value (key_n) and the inner most values, attach to a single string, and then attach it to a string that contains all the single strings combined (hopefully that makes sense).

big_milon = {'key_1' : {'{key1':'dasdadsad', 'key2': 'hat', 'key3':'cat'},
         'key_2' : {'key1':'fat', 'key2': 'pat','key3':'lat'}}

def string_getter():
    prop_string = ''
    full_list = ''
    for i,j in big_milon.items():
        prop_list = []
        itemcode = i
        prop = list(j.values())
        for l in range(len(prop)):
            prop_string =  prop_string +'\t'+ "{" + prop[l] + "}"
        full_list = full_list + itemcode + prop_string + '\n'
        prop_string = ''
    return full_list

print(string_getter())

It works fine, except that sometimes, instead of it looking like there is one tab between the values, it will look like two tabs or no tabs

e.g.

print(string_getter())

enter image description here

Anthon
  • 69,918
  • 32
  • 186
  • 246
RonaldB
  • 207
  • 1
  • 10
  • So you don't like the way tabs are printed, and we can ignore all the code? How do you want it to look? That looks correct for tabstop=4. – Kenny Ostrom Dec 09 '16 at 13:34
  • 1
    This is not a bug - this is expected behavior of tab (move cursor to next multiply of N). If you want consistent tabulation, use string formatting options or dedicated library, e.g. `tabulate`. – Łukasz Rogalski Dec 09 '16 at 13:40
  • 2
    Possible duplicate of [Python - Printing a dictionary as a horizontal table with headers](http://stackoverflow.com/questions/17330139/python-printing-a-dictionary-as-a-horizontal-table-with-headers) – Łukasz Rogalski Dec 09 '16 at 13:40
  • @ŁukaszRogalski is right, but need calculate every value size for don't reached initial offset value. – dsgdfg Dec 09 '16 at 13:42

1 Answers1

3

\t is not for spacing, it's meant for tabulating. \t (or tabulation), by default, skips to the next next column multiple of 8... (+ 1 in reality, as we start counting at 1). So, if you're in columns 1-8, a \t will skip to column 9. If you're in 9-16, it'll skip to 17, etc.

At least, that's the old signification - from mechanical typewriters and teletypes. (even there is some leeway: Some mechanical typewriters allowed settings of the tab columns)

The dots are spaces, the arrows are the result of tab characters, tabs size set to 8The dots are spaces, the arrows are the result of tab characters, tabs size set to 8

Modern computers and editor permit redefinition of the the tab size - generally in Preferences. Frequently they also permits automatically change tabs (\t) into spaces, and the other way around.

Generally, this has made the use of tabs into a nightmare. You can write a text using tabs (eg a program), but if you send it to someone, most like he/she will see the text differently. Here are some examples of 'words' separated by tabs and different settings:

Tab size set to 8Tab size set to 8

Tab size set to 2Tab size set to 2

Tab size set to 4Tab size set to 4

Exception to this is the use of \t as a data separator (more or less like you are doing). There's still some measure of order in the output, unless some of the words are longer that 8 characters.

On the other hand, languages like Python, which depend on spacings to format code, suffer particularly from this and recommend not changing the tab size from 8. And Python 3 apparently even disallows mixing tabs and spaces to avoid more confusion.

To making things even worse, tabs in some contexts have other meanings: like in stackoverflow.com, where tabs change 'area'. If I press tab now, I'll land in other part of the screen. Here are some usage guidelines for tabs in computer use.

jcoppens
  • 5,306
  • 6
  • 27
  • 47