Print Format & End in Python

Asked

Viewed 1,027 times

0

I’m creating an application that connects to db Sqlite, query and returns the column of the table in question, the problem is that the returned result without formatting comes like this : ( u'Field 1, Field 2' ), to print I tried to use a for inside for and printing each field with a print ( '{0} {1} ... '.format ( ... ) ) , and a end = '\n' at the end to be well formatted, but Pyhton does not accept and I need this to be very dynamic because the number of items can vary with each created db ( User chooses fields and types ), I searched a little but I did not find a good way to print this data, would be grateful if you could send references or explanations.

print ( '{0} {1} ... '.format ( ... ), end = '\n' )

  • 1

    Seria this that you need.

  • No, because in this example you sent it specifies how many items will be printed : 'Nome : {} Idade : {} Cpf : {}'.format ( campo [1], campo [2], campo [3] ) and in my case I have no way of knowing how many fields the user added, so if I put ' static ' in this way the application breaks into another db that has more fields.

  • 1

    Vc needs to concatenate the string dynamically?

  • 1

    Give me an example of data entry, and the output you want?

  • Entree : ( 'Table-Name', ( 'Name text', 'Age integer' ... n ) ), exit : Darth Vader 300 ..., yes, concatenate her dynamically.

  • Then you will only display the values?

  • What you call "input", is the return of the function that reads the table? Is it a tuple of tuples? It’s a bit confusing, it would be interesting if you put the code of this function.

  • I will only display, I am not in the notbook that the code is at this time, that ' input ' are the arguments that I have to pass to the table ( Information being added in the table ), the output are the values that are obtained by a select no cursor, summarizing ... don’t mind Db, just printing, I just need to print it here ( u ' Bla, Bla, Bla ' ) in this way : Bla Bla Bla

Show 3 more comments

1 answer

2


I adapted this code based in this answer soen.

If your data entry is the column values that were selected in a query. It is possible to format the output independent of the specified number of columns:

valores = ["gato", "[email protected]", "26", "Vila dos gatos"]

print(' '.join('{}'.format(c) for i, c in enumerate(valores, 1)))

Exit:

cat [email protected] 26 Street of the cats

In this example he uses the join and the function enumerate to format the data.

Editing

An adjustment suggested by the jsbueno to improve and optimize the routine:

print(' '.join(str(valor) for valor in valores))

Exit

cat [email protected] 26 Street of the cats

Comment from jsbueno:

Notice that you don’t need the enumerate there: it will create a number corresponding to each item, but you are formatting a string of each time - then with a single parameter. (If you were to use the number it would be just put "{0}" in the string inside the Join). See that you don’t even uses the variable "i" which is where the value of the enumerate goes. Best there is simply: print(' '.join(str(valor) for valor in valores)) (already that goes nothing beyond the value in the string, "str" is shorter than call the method "format")

  • Note that you don’t need the enumerate there: it will create a number corresponding to each item, but you are formatting one string at a time - then with a single parameter. (If you were to use the number you would only put "{0}" in the string inside the Join). Note that you don’t even use the variable "i" which is where the value of the enumerate goes. : print(' '.join(str(valor) for valor in valores)) (since nothing goes beyond the value in the string, "str" is shorter than calling the method "format")

  • @jsbueno edited ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.