Andromedae93 Andromedae93 - 1 month ago 13
Python Question

Replace value with two arrays numpy

I need your help in order to get a solution with my script.

I have 2 arrays :

==> The first one get 1 column which is named

code_client
. We will call the file :
excel.txt
.
==> The second one has 2 columns :
rowid
and
code_client
. We will call the file :
BDD.txt
.

I would like to replace in the first file the value by rowid associated with the second file.

First file looks like :

code_client
208
1643
1595
2607
2608
2470
1547
481
226
558


Second file looks like :

rowid,code_client
1,1
2,2
3,4
4,5
5,6
6,7
7,8
8,9
9,10
10,11
11,12
12,13
13,14
14,15


This is my script :

# Script permettant de remplacer le code_client par le rowid de la BDD

# coding: utf8

import numpy as np

# Importation du fichier excel

excel = np.loadtxt('/Users/valentinjungbluth/Desktop/excel.txt',
dtype = int)

#print excel

BDD = np.genfromtxt('/Users/valentinjungbluth/Desktop/BDD.txt',
dtype = [('rowid','i8'),('code_client','i8')],
delimiter = ',')

for row1 in excel :
if excel == BDD['code_client'] :
print 'oui'
else :
print 'non'


I don't know how I can associate values. I think I have to read rows from the
excel file
and compare with the
BDD file
. If I get the same number between
code_client from excel file
with
BDD file
, I replace
code_client
from
excel file
by
rowid
from
BDD.file
. Else I continue.

Is it possible to get some help ?

Thank you !

EDIT : EXPECTED FILE

The first file have to become :

code_client ==> rowid
208 ==> this code_client in the second file gives rowid = 183
1643 ==> this code_client in the second file gives rowid = 1498
1595 ==> ...
2607
2608
2470
1547
481
226
558

Answer

I'm not sure what numpy does, but I guess here's what you want

dictionary = dict()
for data in BDD:
    # We are building a dictionary to associate the code_client with rowid
    dictionary[data['code_client']] = data['rowid']
for code_client in excel:
    # From the code_client, you can get the rowid from the dictionary
    print code_client, "=>", dictionary.get(code_client, None) # Do what you want

I'm assuming your BDD is a list of dict, with keys are rowid and code_client; and your excel is a list of values

Comments