BrianG
BrianG

Reputation: 11

Problem to pass csv file into a function in Python

I'm finally asking for help for a small project I just can't complete.

I work on Linux Ubuntu 16.04 and Python 2.7.12 or Python 3.5.2

That's pretty simple:

I have a csv file containing thousands of bitcoin addresses in that format :

12xApR3LKKobfMtPyorsyfpdQt51C1242P
1MqadAMYmwqvJPEYR5UhWHGhupuiXdQ5Pg
1P66WyUGcuPGTKH1ECwmrXoASPR1WP5dfa
1LWb1YtSoQ7m8RreJAEyasHK61jsQqMHGD
1BZnbtBXAKtjrA1suinXrf1s6pjhjPA7oG
166goQjUX3Lh3aN9SEXgcRwpPSBoJWtdDu
1C3pvanq8ZHDGrjfxQU7x8D13DS7d9XjLF

I would like to convert them in HASH160 using this function that actually work for sure:

adr160 = base58.b58decode_check(adr58).encode('hex')[2:]

and write the result in the same format in another text or csv file.

88374DB2A14DEA2925B19AF4D4AD84EB94FDE409
156732EBD650DF4AC212F5C8DE5DFBA4AE588B59
E4928C9EFDE55B17603D9539FB649D9457C293EB
...

I've tried so many things and yet each time there is a problem preventing me from succeeding.

here are my 2 last attempts:

import base58
import csv

adr58 =''

with open('sample.csv', 'rb') as csvfile: 
    adr58 = csv.reader(csvfile)
    adr160 = base58.b58decode_check(adr58).encode('hex')[2:]

with open("Output.txt", "a+") as text_file:
    text_file.write(adr160)

result :

" AttributeError: '_csv.reader' object has no attribute 'rstrip' "

and the second attempt was:

import base58
import pandas as pd

adr58 = pd.read_csv('sample.csv')


def convert(adr58):
    print ("the address is: ")
    adr160 = base58.b58decode_check(adr58).encode('hex')[2:]

    f = open("hashed.txt", "w")
    f.write(adr160)
    f.close()

convert(adr58)

result :

" AttributeError: 'DataFrame' object has no attribute 'rstrip' "

How can I do that in a simple way? I'm at a total loss here.

Thanks in advance for your help guys.

Upvotes: 1

Views: 187

Answers (1)

Joe
Joe

Reputation: 889

You have to loop through the value per row of the DataFrame, and not on the DaataFrame itself:

import base58
import pandas as pd

df= pd.read_csv('sample.csv')



def convert(adr58):
    adr58_list = []
    for i in range(len(df)):
        print ("the address is: ", df.iloc[:,i])
        adr58_list.append(base58.b58decode_check(df.iloc[:,i]).encode('hex')[2:])

    odf = pd.DataFrame()
    odf['column_name'] = adr58_list
    odf.to_csv('hashed.csv', index = False)

convert(df)

Upvotes: 0

Related Questions