如何使用Python从二进制代码创建PDF文件?(How do I create a PDF file from a binary code using Python?)
我试图用Python为每封电子邮件发送自己的PDF文件。 我能够发送自己的PDF文件的二进制代码,但我无法从这个二进制代码重建PDF文件。
以下是我如何获取PDF文件的二进制代码:
file = open('code.txt', 'w') for line in open('somefile.pdf', 'rb').readlines(): file.write(str(line)) file.close()
以下是我尝试从二进制代码创建PDF文件的方法:
file = open('new.pdf', 'wb') for line in open('code.txt', 'r').readlines(): file.write(bytes(line)) file.close()
然后我收到这个错误:
Traceback(最近的最后一次调用):file.write(bytes(line))中的文件“something.py”,第3行TypeError:不带编码的字符串参数
我做错了什么?
I am trying to send myself PDF files per E-mail with Python. I am able to send myself the binary code of a PDF file, but I am not able to reconstruct the PDF file from this binary code.
Here is how I obtain the binary code of a PDF file:
file = open('code.txt', 'w') for line in open('somefile.pdf', 'rb').readlines(): file.write(str(line)) file.close()
Here is how I try to create a PDF file from the binary code:
file = open('new.pdf', 'wb') for line in open('code.txt', 'r').readlines(): file.write(bytes(line)) file.close()
I then recieve this error:
Traceback (most recent call last): File "something.py", line 3, in file.write(bytes(line)) TypeError: string argument without an encoding
What did I do wrong?
原文:https://stackoverflow.com/questions/43019315
最满意答案
嗯,在我看来,你需要设置
cascade
选项。 删除行时,cascade
选项会删除与FK相关的任何内容。 这意味着当删除帖子时, 与该帖子相关的所有评论也将被删除。CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, `postid` int(11) UNSIGNED NOT NULL, `user` varchar(40) NOT NULL, `texto` varchar(3000) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`postid`) REFERENCES posts (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ;
让我知道这个是否奏效。
干杯!
Hmm it seems to me you need to set the
cascade
option. Thecascade
option deletes anything related to the FK when the row is deleted. This means that when a post is deleted all comments related to that post will be deleted as well.CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, `postid` int(11) UNSIGNED NOT NULL, `user` varchar(40) NOT NULL, `texto` varchar(3000) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`postid`) REFERENCES posts (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ;
Let me know if this works.
Cheers!
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
您的数据类型不匹配。 在第一张表中 `specification_variant_parent_id` int(11) unsigned NOT NULL AUTO_INCREMEN 您声明的数据类型是unsigned int 在第二个表中,外键是 `specification_variant_parent_id` smallint(5) unsigned NOT NULL 并且数据类型是unsigned smallint 使它们都具有相同的数据类型。 Your datatypes do not mat ...
-
这是我能想到的最后一件事。 def self.down execute 'ALTER TABLE threads DROP FOREIGN KEY user_id' end 老一 这些应该工作:) remove_foreign_key :threads, { :column => :user_id } 要么 remove_foreign_key('threads', 'user_id') 要么 remove_foreign_key(:threads, :user_id) 切赫 This th ...
-
嗯,在我看来,你需要设置cascade选项。 删除行时, cascade选项会删除与FK相关的任何内容。 这意味着当删除帖子时, 与该帖子相关的所有评论也将被删除。 CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, `postid` int(11) UNSIGNED NOT NULL, `user` varchar(40) NOT NULL, `texto` varch ...
-
我建议你将表单的post/:post_id/comments/:comment_id做成你的url,以便它们是明确的。 你仍然可以在你的URL中使用递增的数字,我会查看诸如acts_as_list gem( https://github.com/swanandp/acts_as_list ) 你可能仍然想在内部使用正常的id,但是你可以使用url中的position来确保它在数据库中被索引。 I would suggest that you make your url of the form post/:p ...
-
首先,您需要从SHOW CREATE表格集中获取外键约束名称。 因为您在创建表时添加了外键。 如果你通过使用alter来添加相同的内容,那么当时你可能会给出一个限制名称,我的意思是说,你给了costrnaint名字,这个名字不是你给出的。 用你可以得到约束名的SHOW CREATE表情节,一旦你得到相同的你可以很容易地删除它。 First of all you need to get foreign key constraint name from SHOW CREATE table episodes. B ...
-
您正在准确描述外键约束在关系数据库中实现并在20世纪80年代末实施的原因。 You are describing exactly the reason why foreign key constraints were implemented in relational databases and enforced in the ending 1980ies.
-
MySQL参考手册显示了FOREIGN KEY命令的允许语法: [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) REFERENCES tbl_name (index_col_name,...) [ON DELETE reference_option] [ON UPDATE reference_option] reference_option: RESTRICT | CASCADE | SET ...
-
它与您的控制器代码无关。 您在架构中添加了一个外部约束数据库,该数据库禁止在有关联记录时删除对象。 在这种情况下,您试图删除附有Comments的Post 。 您需要更改架构并更新外键定义,以指示数据库在这种情况下要执行的操作。 具体来说,您可能希望删除级联上的关联记录。 理论上,您可以使用Post模型中的dependent: :destroy Rails设置来删除级联中的Comment ,但是因为您有一个外键,所以这不是一个好主意。 如果在这种情况下将任务委托给数据库,它会更快更好。 It has not ...