Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想从输出文件中删除u_Python - Fatal编程技术网

Python 我想从输出文件中删除u

Python 我想从输出文件中删除u,python,Python,我有python生成的输出 (u'PK_LampFactoryTest',) (u'DF__Thermosta__Enabl__7B264821',) (u'filestream_tombstone_2073058421',) (u'RetentionSettings',) (u'Alarms',) (u'syscommittab',) sqlquery='从sys.objects中选择名称' cursor.execute(sqlquery) u只是表示它是unicode。 一个简单的解决方法

我有python生成的输出

(u'PK_LampFactoryTest',)
(u'DF__Thermosta__Enabl__7B264821',)
(u'filestream_tombstone_2073058421',)
(u'RetentionSettings',)
(u'Alarms',)
(u'syscommittab',)
sqlquery='从sys.objects中选择名称' cursor.execute(sqlquery)


u只是表示它是unicode。 一个简单的解决方法是

for row in cursor:
     print(row.encode('ascii', 'ignore'))
但这并不理解代码的整体情况,只会使打印语句看起来更漂亮(通过对字符串进行编码)

试试:

import json, ast

for row in cursor:
    print(ast.literal_eval(json.dumps(row)))

u的可能重复只是表示它是Unicode,而您的
是一个元组。如果您只是打印它,Python将打印元组的
repl
,也就是说,您必须在Python源代码中键入什么来创建等效的数据结构。这包括
是一个包含Unicode字符串的1元组。如果您不喜欢格式,请不要要求
print()
打印整个数据结构。只需打印您想要查看的数据:例如,
print(row[0])
@BoarGules好的,明白了。我想获得schema的详细信息。我的意思是没有任何数据的db结构,所以如果我将打印(第[0]行)。它能涵盖所有的费用吗schema@A.Ranjan对不起,这个问题是DBMS特有的。在其他DBMS中,该表可能被称为其他表,也可能根本不是表。因此,我不确定您的查询返回什么,或者您的代码应该如何处理它得到的内容。当然,我可以猜测,但以我的经验来看,经过教育的猜测还不够具体。它删除了u,但给出了['DF_upluginapp_uadmin_u7908f585']['ServiceBrokerQueue']['LampFactoryTest']我只想要名称,没有特殊字符。有没有办法。没有特殊字符是什么意思?你能举个例子吗?['sysrscols']在这里,我不希望括号和冒号只是名称。我已经使用python脚本从sql Server获取架构的详细信息,如果在
print
语句中用
row[0]
替换
row
,这会不会有帮助?
import json, ast

for row in cursor:
    print(ast.literal_eval(json.dumps(row)))