From my database, I'm returning a list to be displayed into my HTML page but my list has this unwanted symbols. I tried using split()
function but it wont work. How can I remove those parenthesis,commas and quotes. Thanks for you help
从我的数据库中,我将返回一个列表以显示在我的HTML页面中,但我的列表中有这些不需要的符号。我尝试使用split()函数,但它不会工作。如何删除这些括号,逗号和引号。谢谢你的帮助
The output is :
输出是:
[('cenomar',), ('cedula',), ('Birth Certificate',), ('Clearance',)]
I want:
我想要:
[cenomar, cedula, Birth Certificate, Clearance]
Here is my python function:
这是我的python函数:
@app.route('/')
def search():
conn=psycopg2.connect("dbname='forms' user='postgres' password='everboy'
host='localhost' port='5432'")
cur=conn.cursor()
cur.execute("SELECT name from form")
rows=cur.fetchall()
print(rows)
return render_template("form.html",rows=rows)
3 个解决方案
#1
6
After This line: rows=cur.fetchall()
add this line
在此行之后:rows = cur.fetchall()添加此行
rows=[i[0] for i in rows]
#2
1
An easy solution would be:
一个简单的解决方案是:
Input: ["('cenomar',)", "('cedula',)", "('Birth Certificate',)", "('Clearance',)"]
输入:[“('cenomar',)”,“('cedula',)”,“('出生证明',)”,“('清除',)”]
rows = ["('cenomar',)", "('cedula',)", "('Birth Certificate',)", "('Clearance',)"]
res = []
for r in rows:
res.append(r[2:-3]) # this line ignores the beginning bracket and open quote and the closing bracket, comma, and close quote
print res
Output: ["cenomar", "cedula", "Birth Certificate", "Clearance"]
输出:[“cenomar”,“cedula”,“出生证明”,“清关”]
What happens is, you iterate over your list, and for each item you just cut off the things you don't need with the use of string manipulation ([n:m]
, where n
is the starting character and m
is the end character, in your case 2
indicates to start the string from the 3 character (ignoring the (
and '
) and -3
indicates cut the string to 3 characters before the end of it (ignoring the )
, '
, and ,
)).
会发生什么,你迭代你的列表,对于每个项目你只是使用字符串操作([n:m],切断你不需要的东西,其中n是起始字符,m是结束字符,在你的情况下,2表示从3个字符开始字符串(忽略(和'),-3表示在结束之前将字符串剪切为3个字符(忽略),'和,))。
EDIT 1
编辑1
I noticed that your input may not be a string as my above answer suggests, if you have a tuple ("smth",)
you can just simply get the 1st element. So the answer would change to:
我注意到你的输入可能不是一个字符串,正如我上面的回答所暗示的那样,如果你有一个元组(“smth”),你可以简单地得到第一个元素。所以答案会改为:
Input: [('cenomar',), ('cedula',), ('Birth Certificate',), ('Clearance',)]
输入:[('cenomar',),('cedula',),('出生证明'),('清除',)]
rows = [('cenomar',), ('cedula',), ('Birth Certificate',), ('Clearance',)]
res = []
for r in rows:
res.append(r[0])
print res
Output: ["cenomar", "cedula", "Birth Certificate", "Clearance"]
输出:[“cenomar”,“cedula”,“出生证明”,“清关”]
The above implementation is written in long for ease of understanding, but as well can be translated into a shorter one liner like so:
上面的实现是为了便于理解而编写的,但也可以翻译成较短的一个内衬,如下所示:
rows = [i[0] for i in rows]
Hope this helps!
希望这可以帮助!
#3
0
Try this one
试试这个
l = [('cenomar',), ('cedula',), ('Birth Certificate',), ('Clearance',)]
b = []
for i in l:
b.append(i[0])
print(b)
#1
6
After This line: rows=cur.fetchall()
add this line
在此行之后:rows = cur.fetchall()添加此行
rows=[i[0] for i in rows]
#2
1
An easy solution would be:
一个简单的解决方案是:
Input: ["('cenomar',)", "('cedula',)", "('Birth Certificate',)", "('Clearance',)"]
输入:[“('cenomar',)”,“('cedula',)”,“('出生证明',)”,“('清除',)”]
rows = ["('cenomar',)", "('cedula',)", "('Birth Certificate',)", "('Clearance',)"]
res = []
for r in rows:
res.append(r[2:-3]) # this line ignores the beginning bracket and open quote and the closing bracket, comma, and close quote
print res
Output: ["cenomar", "cedula", "Birth Certificate", "Clearance"]
输出:[“cenomar”,“cedula”,“出生证明”,“清关”]
What happens is, you iterate over your list, and for each item you just cut off the things you don't need with the use of string manipulation ([n:m]
, where n
is the starting character and m
is the end character, in your case 2
indicates to start the string from the 3 character (ignoring the (
and '
) and -3
indicates cut the string to 3 characters before the end of it (ignoring the )
, '
, and ,
)).
会发生什么,你迭代你的列表,对于每个项目你只是使用字符串操作([n:m],切断你不需要的东西,其中n是起始字符,m是结束字符,在你的情况下,2表示从3个字符开始字符串(忽略(和'),-3表示在结束之前将字符串剪切为3个字符(忽略),'和,))。
EDIT 1
编辑1
I noticed that your input may not be a string as my above answer suggests, if you have a tuple ("smth",)
you can just simply get the 1st element. So the answer would change to:
我注意到你的输入可能不是一个字符串,正如我上面的回答所暗示的那样,如果你有一个元组(“smth”),你可以简单地得到第一个元素。所以答案会改为:
Input: [('cenomar',), ('cedula',), ('Birth Certificate',), ('Clearance',)]
输入:[('cenomar',),('cedula',),('出生证明'),('清除',)]
rows = [('cenomar',), ('cedula',), ('Birth Certificate',), ('Clearance',)]
res = []
for r in rows:
res.append(r[0])
print res
Output: ["cenomar", "cedula", "Birth Certificate", "Clearance"]
输出:[“cenomar”,“cedula”,“出生证明”,“清关”]
The above implementation is written in long for ease of understanding, but as well can be translated into a shorter one liner like so:
上面的实现是为了便于理解而编写的,但也可以翻译成较短的一个内衬,如下所示:
rows = [i[0] for i in rows]
Hope this helps!
希望这可以帮助!
#3
0
Try this one
试试这个
l = [('cenomar',), ('cedula',), ('Birth Certificate',), ('Clearance',)]
b = []
for i in l:
b.append(i[0])
print(b)