gpt4 book ai didi

python - 在 python 中模拟类似文件的行为

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:11 24 4
gpt4 key购买 nike

我正在编写一个脚本,其中我必须将 SQL 数据库中的一些表列转储到文件中,并通过 FTP 传输它。

因为转储可能会变得非常大,所以我的想法是编写一个 FakeFile,它从其 readline 中的游标中逐行查询。方法并将其传递给ftplib.FTP.storlines .

这是我到目前为止所拥有的:

import ftplib
import MySQLdb

def MySQLFakeFile(object):
'''
Simulates a read-only file, which dumps rows on demand.
Use this, to pass it to the FTP protocol to make the dump more efficient,
without the need to dump it somewhere and copy it over the net afterwords
'''
def __init__(self, cursor, delimeter, table_name, query):
self.cursor = cursor
self.delimeter = delimeter
self.table_name = table_name
#query is something along select ... FROM %s
self.cursor.execute(query, table_name)
self._has_written_index = False
#file attrs
self.closed = False
self.name = table_name + ".csv"
self.encoding = "utf-8"
self.mode = "r"


def close(self):
self.cursor.close()
self.closed = True

def flush(self):
'''No-OP'''
pass

def read(self, size):
pass

def readline(self, size):
if not self._has_written_index:
ret = []
for desc in self.cursor.description:
ret.append(desc[0])
self._has_written_index = True
else:
ret = self.cursor.fetchone()
if not ret:
return None

s = ""
for col in ret:
s += str(col) + self.delimeter
return s + "\n"

def readlines(self, size):
ret = []
line = self.readline()
while line:
ret.append(line)
line = self.readline()

def write(self, string):
raise Exception("cannot write to a MySQLFakeFile")

def writelines(self, lines)
raise Exception("cannot write to a MySQLFakeFile")

db = MySQLdb("host", "user", "pass", "db")
ftp = ftplib.FTP("host", "user", "pass")
fakeFile = MySQLFakeFile(db.cursor(), ";", "tableName", "SELECT * FROM %s")
ftp.storlines("STOR tableName.csv", fakeFile)

给我

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/ftplib.py", line 496, in storlines
if len(buf) > self.maxline:
TypeError: object of type 'NoneType' has no len()

我做错了什么,NoneType是什么?在这里?

最佳答案

当到达行尾时,您的 readline 返回 None 而不是空字符串 ""

您的阅读行不会返回任何内容。

def readlines(self, size):
ret = []
while True:
line = self.readline()
if not line:
break
ret.append(line)
return ret

关于python - 在 python 中模拟类似文件的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23722560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com