Bitcoin address convert to RIPEMD160 - python

Why the result is not saved RIPEMD160.txt gives an error
I can see on the processor that the code is working but the file is empty
I always get the same
IndentationError: unindent does not match any outer indentation level
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import base58
def count_lines(file):
return sum(1 for line in open(file, 'r'))
def convert(file_in,file_out,nom):
print("===========File input -> " + file_in)
print("===========File output -> " + file_out)
i = 0
line_10 = 100000
ii = 0
f = open(file_in,'r')
fw = open(file_out,'a')
while i <= nom:
if (i+ii) == nom:
print("\n Finish")
break
if line_10 == i:
print("Error - {} | Total line -> {}".format(ii,line_10),end='\r')
line_10 += 100000
try:
adr58 = f.readline().strip()
adr160 = base58.b58decode_check(adr58).hex()[2:]
except:
ii +=1
else:
fw.write(adr160+'\n')
i += 1
f.close()
fw.close()
if __name__ == "__main__":
if len (sys.argv) < 3:
print ("error")
sys.exit (1)
if len (sys.argv) > 3:
print ("error")
sys.exit (1)
file_in = sys.argv[1]
file_out = sys.argv[2]
line_count = count_lines(file_in)
print("all lines -> " + str(line_count))
convert(file_in,file_out,line_count)
print('Finish')

because you are not writing anything to the file and your code is not formatted correctly.
import base58
def base58_to_dec(addr):
dec = 0
for i in range(len(addr)):
dec = int(dec * 58 + b58.index(addr[i]))
return(dec)
def dec_to_byte(dec):
out = ''
while dec != 0:
remn = mpf(dec % 256)
dec = mpf((dec - remn) / 256)
temp = hex(int(remn))
if len(temp) == 3:
temp = '0' + temp[-1]
else:
temp = temp[2:]
out = temp + out
return (out)
def decode(addr):
b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
dec = base58_to_dec(addr)
out = dec_to_byte(dec)
return (out)
with open('addresses.txt', 'r') as f, \
open('RIPEMD160.txt', 'a') as i:
for addr in f:
addr = base58.b58decode_check(addr).encode('hex')[2:]
ads = decode(addr)
i.write(ads)
i.close()

The process of generating bitcoin address is like this
public_key=>sha256(sha256(public_key))=>RIPEMD160_address=>base58_address
so there is no need for other procedures but simply reverse base58 to rmd160 like below
import base58
i =open('RIPEMD160.txt', 'a') #open file with append mode on
with open('addresses.txt', 'r') as f:#open files with addresses
for addr in f:
addr = addr.strip()#remove trailing space and newline character
rmd160 = base58.b58decode_check(str(addr)).encode('hex')[2:]#notice str forcing addr to be a string
i.write(rmd160+"\n")
i.close()
f.close()

Related

File operations - filtering data from txt file in python

This code works fine but is too big, i would like to know if there is any other way to write this code to make it shorter.
import openpyxl as excel
PATH = "/home/Fathima/workspace/training/"
ACCESS_LIST = []
def READ_CONFIG():
FROM_ZONE = ""
TO_ZONE = ""
POLICY_NAME = ""
SOURCE_ADDR = ""
DESTINATION_ADDR = ""
PORT = ""
global PATH
global ACCESS_LIST
count = 0
CONFIG_PATH=PATH+"hofwdcn05dcn_20210216(2).txt"
fh = open(CONFIG_PATH, 'r')
CONFIG_LINES=fh.readlines()
config_lines_cnt = len(CONFIG_LINES)
while count < config_lines_cnt:
line = CONFIG_LINES[count].strip()
if len(line) > 0:
line_to_array = line.split(' ')
if line.startswith('from-zone '):
FROM_ZONE = line_to_array[1]
TO_ZONE = line_to_array[3]
elif line.startswith('policy '):
POLICY_NAME = line_to_array[1]
elif line.startswith('source-address '):
SOURCE_ADDR = line_to_array[1].replace(";", "")
elif line.startswith('destination-address '):
DESTINATION_ADDR = line_to_array[1].replace(";", "")
elif line.startswith('application '):
PORT = line_to_array[1].replace(";", "")
elif line.startswith('then {'):
count = count+1
line = CONFIG_LINES[count].strip()
if line == "permit;":
dummy = { 'FROM_ZONE' : FROM_ZONE,'TO_ZONE' : TO_ZONE,'POLICY_NAME' : POLICY_NAME,'SOURCE_ADDR' : SOURCE_ADDR,'DESTINATION_ADDR' : DESTINATION_ADDR,'PORT' : PORT}
ACCESS_LIST.append(dummy)
FROM_ZONE = ""
TO_ZONE = ""
POLICY_NAME = ""
SOURCE_ADDR = ""
DESTINATION_ADDR = ""
PORT = ""
count +=1
#MAIN PROGRAM STARTS FROM HERE
READ_CONFIG()
print(ACCESS_LIST)
Here i have a huge file and need the output appearing as below format
[{
from-zone:
to-zone:
policy:
source-address:
destination-address:
application:
},{
from-zone:
to-zone:
policy:
source-address:
destination-address:
application:
}]
There is a separate related site for a review of working code i.e. StackExchange Code Review
That said, below is a more Pythonic code flow. I didn't change conditionals since they are easy to follow.
Main Changes
Eliminate globals (discouraged--only for special needs)
Use file context manager (i.e. use 'with block' on file open)
Iterate through file rather than read the entire file (allows processing arbitrary file size)
Use Python variable and function naming convention i.e. PEP 8
Remove import openpyxl (unused)
Code
def read_config(path):
from_zone, to_zone, policy_name, source_addr, destination_addr, port = [''] * 6
access_list = []
with open(path + "hofwdcn05dcn_20210216(2).txt", 'r') as fh:
for line in fh:
line = line.strip()
if line:
line_to_array = line.split(' ')
if line.startswith('from-zone '):
from_zone = line_to_array[1]
to_zone = line_to_array[3]
elif line.startswith('policy '):
policy_name = line_to_array[1]
elif line.startswith('source-address '):
source_addr = line_to_array[1].replace(";", "")
elif line.startswith('destination-address '):
destination_addr = line_to_array[1].replace(";", "")
elif line.startswith('application '):
port = line_to_array[1].replace(";", "")
elif line.startswith('then {'):
line = next(fh).strip() # Gets next line in file
if line == "permit;":
access_list.append({'FROM_ZONE': from_zone,
'TO_ZONE': to_zone,
'POLICY_NAME': policy_name,
'SOURCE_ADDR': source_addr,
'DESTINATION_ADDR': destination_addr,
'PORT': port})
from_zone, to_zone, policy_name, source_addr, destination_addr, port = [''] * 6
return access_list
access_list = read_config("/home/Fathima/workspace/training/")
print(access_list)

Error in wikipedia subcategory crawling using python3

Hello Community Members,
I am getting the error NameError: name 'f' is not defined. The code is as follows. Please help. Any sort of help is appreciated. I have been strucked onto this since 3 days. The code is all about to extract all the subcategories name of wikipedia category in Python 3.
I have tried both the relative and absolute paths.
The code is as follows:
import httplib2
from bs4 import BeautifulSoup
import subprocess
import time, wget
import os, os.path
#declarations
catRoot = "http://en.wikipedia.org/wiki/Category:"
MAX_DEPTH = 100
done = []
ignore = []
path = 'trivial'
#Removes all newline characters and replaces with spaces
def removeNewLines(in_text):
return in_text.replace('\n', ' ')
# Downloads a link into the destination
def download(link, dest):
# print link
if not os.path.exists(dest) or os.path.getsize(dest) == 0:
subprocess.getoutput('wget "' + link + '" -O "' + dest+ '"')
print ("Downloading")
def ensureDir(f):
if not os.path.exists(f):
os.mkdir(f)
# Cleans a text by removing tags
def clean(in_text):
s_list = list(in_text)
i,j = 0,0
while i < len(s_list):
#iterate until a left-angle bracket is found
if s_list[i] == '<':
if s_list[i+1] == 'b' and s_list[i+2] == 'r' and s_list[i+3] == '>':
i=i+1
print ("hello")
continue
while s_list[i] != '>':
#pop everything from the the left-angle bracket until the right-angle bracket
s_list.pop(i)
#pops the right-angle bracket, too
s_list.pop(i)
elif s_list[i] == '\n':
s_list.pop(i)
else:
i=i+1
#convert the list back into text
join_char=''
return (join_char.join(s_list))#.replace("<br>","\n")
def getBullets(content):
mainSoup = BeautifulSoup(contents, "html.parser")
# Gets empty bullets
def getAllBullets(content):
mainSoup = BeautifulSoup(str(content), "html.parser")
subcategories = mainSoup.findAll('div',attrs={"class" : "CategoryTreeItem"})
empty = []
full = []
for x in subcategories:
subSoup = BeautifulSoup(str(x))
link = str(subSoup.findAll('a')[0])
if (str(x)).count("CategoryTreeEmptyBullet") > 0:
empty.append(clean(link).replace(" ","_"))
elif (str(x)).count("CategoryTreeBullet") > 0:
full.append(clean(link).replace(" ","_"))
return((empty,full))
def printTree(catName, count):
catName = catName.replace("\\'","'")
if count == MAX_DEPTH : return
download(catRoot+catName, path)
filepath = "categories/Category:"+catName+".html"
print(filepath)
content = open('filepath', 'w+')
content.readlines()
(emptyBullets,fullBullets) = getAllBullets(content)
f.close()
for x in emptyBullets:
for i in range(count):
print (" "),
download(catRoot+x, "categories/Category:"+x+".html")
print (x)
for x in fullBullets:
for i in range(count):
print (" "),
print (x)
if x in done:
print ("Done... "+x)
continue
done.append(x)
try: printTree(x, count + 1)
except:
print ("ERROR: " + x)
name = "Cricket"
printTree(name, 0)
The error encountered is as follows.
I think f.close() should be content.close().
It's common to use a context manager for such cases, though, like this:
with open(filepath, 'w+') as content:
(emptyBullets,fullBullets) = getAllBullets(content)
Then Python will close the file for you, even in case of an exception.
(I also changed 'filepath' to filepath, which I assume is the intent here.)

ValueError: could not convert string to float: ENTRIESn_hourly

so i am trying to write a map/reduce code to analyze the total entries by unit of the NYC subway.
my mapper code generated a .txt file as demanded by the project.
import sys
def mapper():
for line in sys.stdin:
data = line.strip().split(",")
if len(data) == 22:
Unnamed,UNIT,DATEn,TIMEn,Hour,DESCn,ENTRIESn_hourly,EXITSn_hourly,maxpressurei,maxdewpti,mindewpti,minpressurei,meandewpti,meanpressurei,fog,rain,meanwindspdi,mintempi,meantempi,maxtempi,precipi,thunder = data
print "{0}\t{1}".format(UNIT,ENTRIESn_hourly)
sys.stdin = open('turnstile_data_master_with_weather.csv')
sys.stdout = open('mapper_result.txt', 'w')
mapper()
the file mapper_result.txt is correct, it's a 2 column file(key/value) of the entries by unit of NYC subway.
so ,then, i went to write de reducer code to sum all the values by unit, as follows:
import sys
def reducer():
entriesTotal = 0
oldKey = None
for line in sys.stdin:
data = line.strip().split("\t")
if len(data) != 2:
continue
thisKey,thisEntry = data
if oldKey and oldKey != thisKey:
print "{0}\t{1}".format(oldKey,entriesTotal)
entriesTotal = 0
oldKey = thisKey
entriesTotal += float(thisEntry)
if oldKey != None:
print "{0}\t{1}".format(oldKey, entriesTotal)
sys.stdin = open('mapper_result.txt')
sys.stdout = open('reducer_result.txt', 'w')
reducer()
ValueError Traceback (most recent call last)
<ipython-input-28-8ec50e7ee920> in <module>()
21 sys.stdin = open('mapper_result.txt')
22 sys.stdout = open('reducer_result.txt', 'w')
---> 23 reducer()
<ipython-input-28-8ec50e7ee920> in reducer()
15 entriesTotal = 0
16 oldKey = thisKey
---> 17 entriesTotal += float(thisEntry)
18 if oldKey != None:
19 print "{0}\t{1}".format(oldKey, entriesTotal)
ValueError: could not convert string to float: ENTRIESn_hourly
maybe it s a problem converting the strings in .txt file in floats.
anyone have an idea?
Ok , so i managed to finish it by using the try...except in the thisEntry.
here is the final code:
import sys
def reducer():
entriesTotal = 0
oldKey = None
for line in sys.stdin:
data = line.strip().split("\t")
if len(data) != 2:
continue
thisKey,thisEntry = data
if oldKey and oldKey != thisKey:
print "{0}\t{1}".format(oldKey,entriesTotal)
entriesTotal = 0
oldKey = thisKey
try:
entriesTotal += float(thisEntry)
except:
print thisEntry
if oldKey != None:
print "{0}\t{1}".format(oldKey, entriesTotal)
sys.stdin = open('mapper_result.txt', 'r')
sys.stdout = open('reducer_result.txt', 'w')
reducer()

Python dictionary search not finding result given a string

I'm writing code for a project and it searches a text file for occurrences of a word on each line. When I use a example text file and search for a word it always prints out "No results for: " even if the word I searched for is in it. Did I setup the dictionary wrong or something?
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 14 11:31:17 2017
#author: Ben Roux
"""
import re
from collections import Counter
stringinput = raw_input("Please enter a filename to open: ")
dictionary = {}
def openFile(stringinput):
try:
filevariable = open(stringinput, 'r')
return filevariable
except IOError:
print("Cannot Find File!")
def readData(stringinput):
filevariable = open(stringinput, 'r')
rawline = filevariable.readline()
line = 1
while (rawline !=""):
pl1 = rawline.replace(",","")
pl2 = pl1.replace("'","")
pl3 = pl2.replace("!","")
pl4 = pl3.replace("-"," ")
pl5 = pl4.replace(".","")
pl6 = re.sub('(\\b[A-Za-z] \\b|\\b [A-Za-z]\\b)', '', pl5)
pl7 = pl6.lower()
checkdictionary = sorted(Counter(pl7.split()).items())
for i in range(len(checkdictionary)):
if checkdictionary[i] in dictionary:
firstvalue = dictionary.get(checkdictionary[i])
newvalue = str(firstvalue) + ", " + str(line)
d1 = {checkdictionary[i]: newvalue}
dictionary.update(d1)
else:
d2 = {checkdictionary[i]: line}
dictionary.update(d2)
rawline = filevariable.readline()
line+=1
def processText(dictionary, searchkey):
if searchkey in dictionary:
print(str(searchkey) + " Appears On Lines: " + (str(dictionary[searchkey])))
else:
print("No results for: " + str(searchkey))
while (True):
try:
openFile(stringinput)
readData(stringinput)
searchkey = raw_input("Enter a keyword to search for: ")
processText(dictionary, searchkey)
break
except IOError:
break
#AK47's answer for changing the if else statement works and this also works:
checkdictionary = sorted(Counter(pl7.split()).items())
change to
checkdictionary = pl7.split()
Update this following code;
if checkdictionary[i][0] in dictionary:
firstvalue = dictionary.get(checkdictionary[i][0])
newvalue = str(firstvalue) + ", " + str(line)
d1 = {checkdictionary[i][0]: newvalue}
dictionary.update(d1)
else:
d2 = {checkdictionary[i][0]: line}
dictionary.update(d2)

how to merge same object in json file using python

1.json file contain many sniffing WIFI packets, I want get the mac address of receiver and transmitter which can be found in the first "wlan" object called "wlan.ra" and "wlan.sa". data[0] is the first WIFI packet.
Q1:
But when I try to print the elements of wlan after json load, it only show the elements of the second "wlan" object so there is no "wlan.ra" and "wlan.sa" in the data.
with open('1.json','r') as json_data:
data = json.load(json_data)
a=data[0]
print a
Q2:
There are two 'wlan' objects in my json file. How can I merge the elements in these two 'wlan' objects into just one 'wlan' object?
The following is my code, but it doesn't work:
with open('1.json','r') as f:
data=json.load(f)
for i in data:
i['_source']['layers']['wlan'].update()
Screenshot of json file:
'''
Created on 2017/10/3
#author: DD
'''
import os
def modify_jsonfile(jsonfile):
'''
replace wlan to wlan1/wlan2
'''
FILESUFFIX = '_new' # filename suffix
LBRACKET = '{' # json object delimiter
RBRACKET = '}'
INTERSETED = '"wlan"' # string to be replaced
nBrackets = 0 # stack to record object status
nextIndex = 1 # next index of wlan
with open(jsonfile, 'r') as fromJsonFile:
fields = os.path.splitext(jsonfile) # generate new filename
with open(fields[0] + FILESUFFIX + fields[1], 'w') as toJsonFile:
for line in fromJsonFile.readlines():
for ch in line: # record bracket
if ch == LBRACKET:
nBrackets += 1
elif ch == RBRACKET:
nBrackets -= 1
if nBrackets == 0:
nextIndex = 1
if (nextIndex == 1 or nextIndex == 2) and line.strip().find(INTERSETED) == 0: # replace string
line = line.replace(INTERSETED, INTERSETED[:-1] + str(nextIndex) + INTERSETED[-1])
nextIndex += 1
toJsonFile.write(line);
print 'done.'
if __name__ == '__main__':
jsonfile = r'C:\Users\DD\Desktop\1.json';
modify_jsonfile(jsonfile)

Categories

Resources