有人知道当我使用 SimpleDocTemplate() 制作 PDF 文档时是否可以删除默认间距。
我希望它从一个角落打印到另一个角落。
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
# Our container for 'Flowable' objects
elements = []
# A large collection of style sheets pre-made for us
styles = getSampleStyleSheet()
# A basic document for us to write to 'rl_hello_table.pdf'
doc = SimpleDocTemplate(response)
# elements.append(Paragraph("Wumpus vs Cave Population Report",
# styles['Title']))
data = [
['Deep Ditch', 50],
['Death Gully', 5000],
['Dire Straits', 600],
['Deadly Pit', 5],
['Deep Ditch', 50],
['Deep Ditch', 50],
['Death Gully', 5000], …Run Code Online (Sandbox Code Playgroud) 为什么这段代码会占用内存?当我运行它时,每个循环都会慢慢消耗更多的内存,而且我有300000个循环.我正在使用Windows和Python 2.6.
def LoadVotes(self):
old_votes=Votes.objects.all()
amount=old_votes.count()
print 'Amount of votes is: ' + str(amount)
c=0
for row in old_votes:
try:
new_id_user=LegacyUserId.objects.get(legacy_id=row._login)
except LegacyUserId.DoesNotExist:
string=" user with old id "+str(row._login)+" does not match new user id /n"
log=open('log_add_old_votes.txt','a')
log.write(string)
continue
try:
new_id_media=LegacyMedia.objects.get(legacy_id=row.media_file_id)
except new_id_media.DoesNotExist:
log_text='old media with ID:'+str(row.media_file_id)+' is not found in relation with new media \n'
log=open('log_add_old_votes.txt','a')
log.write(log_text)
continue
mo=MediaObject.objects.get(pk=new_id_media.object_id)
new_votes_item, created=Mark.objects.get_or_create(user=new_id_user.user, media_object=mo, defaults={'mark':int(row.rate)*2})
c=c+1
i=amount-c
print '\rRemain:',
stdout.write("%d" % i)
stdout.flush()
Run Code Online (Sandbox Code Playgroud) 我有:
class C:
aaa=2
class B:
def __init__ (self,name):
self.name
self.value
Run Code Online (Sandbox Code Playgroud)
我如何定义类,C所以当我动态地将属性设置为实例时,它会创建类的属性实例B.和属性类的名称B必须具有属性name的类,新属性的名称相同字符串C和属性value的B实例必须有什么价值,在类的实例的新属性集C.
必须给我这个结果:
>> c=C()
>> c.whatever= 'strinstrinsstring'
>> isinstance(c.whatever,B)
True
>> c.whatever.value
'strinstrinsstring'
>>c.whatever.name
'whatever'
Run Code Online (Sandbox Code Playgroud) 我想在Django中使用ORDER BY FIELD()(MySQL).Django支持吗?
所以在MySQL中它看起来像:
SELECT * FROM fruit
ORDER BY FIELD(name, 'Banana', 'Apple', 'Pear', 'Orange'), variety;
Run Code Online (Sandbox Code Playgroud)
我想在django ORM中使用它.我怎样才能做到这一点.我可以这样做吗order_by()?或以extra()某种方式使用.
不这样工作:
Fruit.objects.all().extra(order_by="FIELD(name, 'Banana', 'Apple', 'Pear', 'Orange')")
我有一个数据库,其name列有数据
'Very big News'
'News'
'something else'
'New Nes'
'Fresh News'
'Something else'
Run Code Online (Sandbox Code Playgroud)
现在给出一串单词,如何找到name字段中是否包含给定字符串中的任何单词?
例如:
我有一个字符串'super very news'.我需要在我的数据库中查看我是否有任何记录,使该name字段包含'super'或'very'或'news'或'super very'或'very news'.
如何通过for循环传递列表中的所有项目.如果迭代不是从第一个元素开始的话.
让我们马上跳到示例:
我们有清单 ['a','b','c','d'].
我想使用for循环遍历此列表中的所有项目.但是如果迭代不是从第一个元素开始,我想从第一个元素开始返回oand.像这样的东西:
lst = ['a','b','c','d']
start_index = 2
for loop_num in range(len(lst)):
item = lst[start_index+loop_num]
print item
Run Code Online (Sandbox Code Playgroud)
它会打印我:
>> c,d
Run Code Online (Sandbox Code Playgroud)
比升起IndexOutOfRange错误
但我希望结果是这样的:
>> c, d, a, b
Run Code Online (Sandbox Code Playgroud)
如果我们将start_index变量更改为1结果,则假设为:
b, c, d, a
Run Code Online (Sandbox Code Playgroud)
的情况下 start_index = 0
结果: a, b, c, d
我想__init__动态地在类方法中创建一堆方法.到目前为止还没有运气.
码:
class Clas(object):
def __init__(self):
for i in ['hello', 'world', 'app']:
def method():
print i
setattr(self, i, method)
Run Code Online (Sandbox Code Playgroud)
比我列表中最适合的方法和调用方法.
>> instance = Clas()
>> instance.hello()
'app'
Run Code Online (Sandbox Code Playgroud)
我希望它打印hello不app.问题是什么?此外,这些动态分配的方法中的每一个都在内存中引用相同的功能,即使我这样做copy.copy(method)
如何在路径中添加字母?
例如,如果我有一个路径'c:\example2\media\uploads\test5.txt'(存储在变量中),但我需要类似的东西r'c:\example2\media\uploads\test5.txt',我怎么能添加字母`r?
因为该函数open()不想打开第一个路径.当我尝试添加路径到功能时,open()它给我一个错误和这样的路径:u'c:\\example2\\media\\uploads\\test5.txt'并说该文件或目录不存在.我该怎么办?
错误看起来像:
[Error 3] The system cannot find the path specified: u'C:\\example2\\media\\upload\\ZipFile.zip'
Run Code Online (Sandbox Code Playgroud)
当我这样打开('c:\ example2\media\uploads\test5.txt')它不起作用.并给我错误(你可以在顶部看到)
我从SQL服务器获得Python程序的响应.这种反应有多大?什么是最大值?它可以达到约100 mb?
在这段代码中:
soup=BeautifulSoup(program.Description.encode('utf-8'))
name=soup.find('div',{'class':'head'})
print name.string.decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
当我尝试打印或保存到数据库时发生错误.
dosnt metter我在做什么:
print name.string.encode('utf-8')
Run Code Online (Sandbox Code Playgroud)
要不就
print name.string
Traceback (most recent call last):
File "./manage.py", line 16, in <module>
execute_manager(settings)
File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/core/management/__init__.py", line 362, in execute_manager
utility.execute()
File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/cluster/dynamic/virtualenv/lib/python2.5/site-packages/django/core/management/base.py", line 222, in execute
output = self.handle(*args, **options)
File "/usr/local/cluster/dynamic/website/video/remmedia/management/commands/remmedia.py", line 50, in handle
self.FirstTimeLoad()
File "/usr/local/cluster/dynamic/website/video/remmedia/management/commands/remmedia.py", line 115, in FirstTimeLoad
print name.string.decode('utf-8')
File "/usr/lib/python2.5/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, …Run Code Online (Sandbox Code Playgroud)