how to delete the u and before the of database table display by python [closed]

2024/7/7 6:22:09

I am trying to use python to create a database and then insert data and display it. However, the output adds a u before every string. What should I do, how can I delete the "u"? the following is the output display:

-----------------------------------------------
| Date         | Time    | Price      |
-----------------------------------------------
(u'31/05/2013', u'11:10', u'$487')
(u'31/05/2013', u'11:11', u'$487')
(u'31/05/2013', u'11:13', u'$487')
(u'31/05/2013', u'11:19', u'$487')

I want the output only shows like

-----------------------------------------------
| Date         | Time    | Price      |
-----------------------------------------------31/05/2013       11:10     $487

I do not want to see the u and the ''.

the following is a part of my code

cursor.execute("CREATE TABLE if not exists table2 (date text, time text, price real)")date=strftime("%d/%m/%Y")
time=strftime("%H:%M")
data1 = [(date,time,eachprice),]
cursor.executemany('INSERT INTO table2 VALUES (?,?,?)', data1)
conn.commit()
#output
print "Showing history for 'ipad mini', from harveynorman"
print "-----------------------------------------------"
print "| Date         | Time    | Price      |"
print "-----------------------------------------------"
for row in cursor.execute('select * from table2').fetchall():print row

so, could anyone can help me figure out how to delete the g and ''

Answer

You are looking at whole tuples with unicode strings; the u'' is normal when showing you a tuple with unicode values inside:

>>> print u'Hello World!'
Hello World!
>>> print (u'Hello World',)
(u'Hello World',)

You want to format each row:

print u' {:<15} {:<8} {:<6}'.format(*row)

See the str.format() documentation, specifically the Format Syntax reference; the above formats 3 values with field widths, left-aligning each value into their assigned width.

The widths are approximate (I didn't count the number of spaces in your post exactly), but should be easy to adjust to fit your needs.

Demo:

>>> row = (u'31/05/2013', u'11:10', u'$487')
>>> print u' {:<15} {:<8} {:<6}'.format(*row)31/05/2013      11:10    $487  

or, using a loop and a sequence of row entries:

>>> rows = [
... (u'31/05/2013', u'11:10', u'$487'),
... (u'31/05/2013', u'11:11', u'$487'),
... (u'31/05/2013', u'11:13', u'$487'),
... (u'31/05/2013', u'11:19', u'$487'),
... ]
>>> for row in rows:
...     print u' {:<15} {:<8} {:<6}'.format(*row)
... 31/05/2013      11:10    $487  31/05/2013      11:11    $487  31/05/2013      11:13    $487  31/05/2013      11:19    $487  
https://en.xdnf.cn/q/120440.html

Related Q&A

python list permutations [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:How to generate all permutations of a list in Python I am given a list [1,2,3] and the task is to create all the possible …

Repeating Characters in the Middle of a String

Here is the problem I am trying to solve but having trouble solving:Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a n…

Remove a big list of of special characters [duplicate]

This question already has answers here:Remove specific characters from a string in Python(27 answers)Closed 5 years ago.I want to remove each of the following special characters from my documents: symb…

How to change a html page from flask to Django [duplicate]

This question already has an answer here:How to specify URLs in Django templates?(1 answer)Closed 7 years ago.I am working on an app that requires changing a flask template to that of Django.How to ch…

How to get the text of Checkbuttons?

Checkbuttons gets generated dynamically and they are getting text from a python list. I need a logic for capturing selected checkbuttons text . As per my research everywhere they are returning the stat…

Working out an equation

Im trying to solve a differential equation numerically, and am writing an equation that will give me an array of the solution to each time point.import numpy as np import matplotlib.pylab as pltpi=np.p…

combine rows and add up value in dataframe

I got a dataframe(named table) with 6 columns labeled as [price1,price2,price3,time,type,volume]for type, I got Q and T, arranged like:QTQTTQNow I want to combine the rows with consecutive T and add up…

How to access a part of an element from a list?

import cv2 import os import glob import pandas as pd from pylibdmtx import pylibdmtx import xlsxwriter# co de for scanningimg_dir = "C:\\images" # Enter Directory of all images data_path = os…

How to get invisible data from website with BeautifulSoup

I need fiverr service delivery times but I could get just first packages(Basic) delivery time. How can I get second and third packages delivery time? Is there any chance I can get it without using Sel…

How to get rid of \n and in my json file

thanks for reading I am creating a json file as a result of an API that I am using. My issue is that the outcome gets has \h and in it and a .json file does not process the \n but keeps them, so the f…