我目前正在研究加密程序,并面临使用Python 3编写非文本文件的问题。
例如,这将起作用(定义了fEncode()):
Public Sub moveRows2()
Dim ws1 As Worksheet, ws2 As Worksheet, ur1 As Range, xRow2 As Long
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
xRow2 = ws2.UsedRange.Row + ws2.UsedRange.Rows.Count 'find 1st blank cell on Sheet2
Set ur1 = ws1.UsedRange
With ur1
.AutoFilter
.AutoFilter Field:=5, Criteria1:="=" 'AutoFilter Col E (blank cells)
'If there are any visible rows
If ws1.Cells(ws1.Rows.Count, ur1.Column).End(xlUp).Row > ur1.Row Then
'Copy-paste visible data from Sheet1 to Sheet2
.Offset(1).Resize(.Rows.Count - 1, .Columns.Count).Copy ws2.Cells(xRow2, 1)
'Delete visible range with data from Sheet1
.Offset(1).Resize(.Rows.Count - 1, .Columns.Count).EntireRow.Delete
End If
.AutoFilter
End With
End Sub
这里textFileName =' C:\ aRandomTextFile.txt'。但是,如果我将它替换为类似于C:\ aRandomImage.png'并替换
text = ''
textFile = open(textFileName, 'r', encoding='utf-8')
for textLine in textFile:
text += textLine
textFile.close()
ciphertext = text
numPassCounter = 0
for password in passwords:
ciphertext = fEncode(ciphertext, password, num_passwords[numPassCounter])
numPassCounter += 1
os.system("copy /y " + textFileName + " " + ciphertextFileName)
ciphertextFile = open(ciphertextFileName, 'w', encoding='utf-8')
ciphertextFile.write(ciphertext)
ciphertextFile.close()
与
ciphertextFile = open(textFileName, 'w', encoding='utf-8')
然后尝试
ciphertextFile = open(textFileName, 'wb')
我得到了
ciphertextFile.write(bytes(str(ciphertext, encoding='utf-8')))
我做错了什么?
答案 0 :(得分:1)
问题在于你制作字节字符串的方式。请考虑以下内容,类似于您正在做的事情:
default_charset = "UTF-8"
或
>>> bytes('banana')
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
bytes('banana')
TypeError: string argument without an encoding
但现在尝试另一种方式:
>>> bytes(str('banana',encoding='utf-8'))
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
bytes(str('banana',encoding='utf-8'))
TypeError: decoding str is not supported
或
>>> bytes('banana'.encode('utf-8')) # redundant, see last example
b'banana'
甚至只是:
>>> bytes(ord(c) for c in 'banana')
b'banana'
所以现在你可以看到>>> 'banana'.encode()
b'banana'
正在取一个字符串,使它成为二进制字符串,将其作为未编码的字符串,然后再尝试从中生成一个字节字符串。
希望有所帮助。
答案 1 :(得分:1)
您是否尝试过var x = [2, 3, 8];
var sums = [];
var sets = [];
function SubSets(read, queued)
{
if( read.length == 4 || (read.length <= 4 && queued.length == 0) )
{
if( read.length > 0 ){
var total = read.reduce(function(a,b){return a+b;},0);
if(sums.indexOf(total)==-1){
sums.push(total);
sets.push(read.slice().sort());
}
}
}
else
{
SubSets(read.concat(queued[0]),queued.slice(1));
SubSets(read,queued.slice(1));
}
}
SubSets([],x);
console.log(sums.sort(function(a,b){return a-b;}));