0

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 : rowidand 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
9
  • You have the row id, you have a list of rows in the first file, isn't the row_id the same as index + 1, you can just call list[row_id - 1] = new_value Commented Oct 27, 2016 at 10:05
  • What's the expected output for the sample? Commented Oct 27, 2016 at 10:09
  • @btquanto No because the first file is not order by ascendant value. I need to keep this order. Furthermore, rowid = 1,2,3 ... and code_client = 1,2,4 etc .. At the end rowid = 2800 and code_client = 3600 Commented Oct 27, 2016 at 10:10
  • It does not matter if it's in ascending, descending or random order. check my answer Commented Oct 27, 2016 at 10:12
  • @Divakar : I added an EDIT part with the output expected ;) Commented Oct 27, 2016 at 10:14

1 Answer 1

1

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
    row_id = dictionary.get(code_client, None)
    print code_client, "=>", row_id # Do what you want

You can also build the dictionary like this:

dictionary = dict([(data['code_client'], data['row_id']) for data in BDD])

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

Sign up to request clarification or add additional context in comments.

4 Comments

Both files are a list of values. I will try your script
It doesn't work. Syntax error with the last line. I will see what doesn't work. To the output file, I juste need to get rowid values in place of code_client values ;)
Yep I will see dictionary lesson ;)
I guess some code_client do not exist in the dictionary, causing a crash, so try dictionary.get(code_client, None) instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.