我的密码
import pymysql
conn=pymysql.connect(host=.................)
curs=conn.cursor()
import csv
f=open('./kospilist.csv','r')
data=f.readlines()
data_kp=[]
for i in data:
data_kp.append(i[:-1])
c = csv.writer(open("./test_b.csv","wb"))
def exportFunc():
result=[]
for i in range(0,len(data_kp)):
xp="select date from " + data_kp[i] + " where price is null"
curs.execute(xp)
result= curs.fetchall()
for row in result:
c.writerow(data_kp[i])
c.writerow(row)
c.writerow('\n')
exportFunc()
data_kp正在读取表名 表的名称如下(string,ex:a000010) 我从这里收集表名。 然后,执行并得到结果。在
我的期望是
(不是3列。。有2000张桌子)
我以为我的代码就快找到答案了。。。但它不起作用。。 我的工作快完成了,但我没能完成这部分。 我在谷歌上搜索了将近10个小时。。 我不知道怎么。。请帮忙
我觉得这部分有问题
^{pr2}$
^{} 方法允许您在输出
csv
文件中编写一个行。这意味着一旦您调用了writerow
方法,该行就会被写入,您将无法返回该行。编写代码时:你是说:
这样,所有内容都将以}之间的交替书写。在
data_kp[i]
和{令人惊讶的是,这并不是我们从你们的实际产出中得到的。我觉得你改变了一些东西。像这样:
^{pr2}$但这并没有完全解决您的问题,很明显:表的名称没有正确显示(每列一个字符),并且它们不是并排的。所以这里有两个问题:
1。在一个单元格中获取表名,并且不拆分
首先,让我们看一下关于^{} 的文档:
但是你的
data_kp[i]
是String
,而不是“iterable ofString
”。这不行!但你也没有出错,为什么?这是因为在python中,String
本身可能被认为是String
的iterable。你自己试试:现在,你可能已经明白了要做什么才能让事情运转起来:
现在您的表名只显示在1个单元格中!但我们还有一个问题。。。在
2。获取并排显示的表名
在这里,这是代码逻辑中的一个问题。你正在浏览你的表名,写下包含它们的行,并期望它们被并排书写,得到日期列!在
您的代码需要重新考虑一下,因为} 模块的
csvwriter
不是用来写列而是用来写行的。然后我们将使用^{zip_longest
函数。有人会问我为什么不使用Python的zip
内置函数:这是因为列的大小并不相同,zip
函数一旦到达最短列表的末尾就会停止!在注意: 这里提供的代码尚未测试,可能包含错误。不过,您应该能够(通过使用我提供的文档)来修复它,以便使其正常工作!祝你好运:)
相关问题 更多 >
编程相关推荐