简体   繁体   English

从文本文件读取时无法正确编码字符串(编码为sha256…)

[英]Cannot correctly encode string when reading from text file (encoding into sha256…)

Basically what I want to accomplish (simplified...): 基本上我想完成的事情(简化...):

I want to make 100 bitcoin addresses from my own password that look kind of like: 我想用自己的密码制作100个比特币地址,如下所示:

password_1 password_2 password_3 密码_1密码_2密码_3

So when I do this in the program, I am getting the correct result: 因此,当我在程序中执行此操作时,将得到正确的结果:

def public_key(src):
    privatekey = (int(hashlib.sha256(src).hexdigest(), 16))
    return generate_address(privatekey)
def private_key(src):
    privatekey = hashlib.sha256(src).hexdigest()
    return str(privatekey)
herewego = "password_1".encode('utf-8')
somevariable = public_key(herewego)
print somevariable 

^ This works as intended...but if I put "password_1" in a txt file and try to read this line, it gives totally different result? ^这按预期工作...但是,如果我在txt文件中放入“ password_1”并尝试读取此行,则结果会完全不同吗?

for addr in file:
 address =  addr.encode('utf-8')
 print public_key(address)

So the issue is obviously that Notepad encodes the text file in say ansi or utf-8, it doesn't matter but the line read from there must be looking different to python than when I enter the " ...." within python? 因此,问题很明显是记事本用ansi或utf-8编码文本文件,这没关系,但是从那里读取的行与python的外观一定要与在python中输入“ ....”时的外观不同? So what coding to use or if it's impossible: what alternative to Notepad? 那么要使用什么编码,或者如果不可能的话:记事本的替代方案是什么? This is for Python 2.7 in windows by the way. 顺便说一下,这是针对Windows中的Python 2.7的。

There might be newline characters, you could try 可能有换行符,您可以尝试

for addr in file:
address =  addr.rstrip('\n').encode('utf-8')
print public_key(address)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM