Sorting and sequencing data from a file

0

I've developed a program that stores a list of ids, so

Butforthedesiredpurposes,thedatashouldtakethesequentialform,sothatthefirstpairofidsissomethinglike:"889926212541448192" becomes 1 and "889919950248448000" becomes 2. That is, the file to get should be something like:

Wherethefirstidconnectswith2,3and6,andtheid4onlywith5,forminganetwork.

Ihavenoexperienceinthisarea,butIcannotfindawaytodothisreading.

Itriedtodosomeprograms,buttheyreadonlyrowandnotcolumnidtoid.Thisdataissavedfollowingthefollowingprogram

importjson

arq=open('ids.csv','w')arq.write('Source'+','+'Target')arq.write("\ n")

list_rede = [] #list to save all ids

with open ('data_twitter.json', 'r') as f:

for line in f:
    lista = []

    tweet = json.loads(line) # reescreve como um dicionário Python 
    lista = list(tweet.keys()) #escreve lista das chaves 

    try:
        if 'retweeted_status' in lista:
            id_rt = json.dumps(tweet['retweeted_status']['id_str'])
            id_status = json.dumps(tweet['id_str'])

            lista_rede.append(tweet['id_str'])
            lista_rede.append(tweet['retweeted_status']['id_str'])

            arq.write( id_status +','+ id_rt )
            arq.write("\n")

        if tweet['quoted_status'] in lista :
            id_rt = json.dumps(tweet['quoted_status']['id_str'])
            id_status = json.dumps(tweet['id_str'])

            lista_rede.append(tweet['id_str'])
            lista_rede.append(tweet['quoted_status']['id_str'])

            arq.write( id_status +','+ id_rt )
            arq.write("\n")
    except:
           continue

arq.close ()

As a result I have a file with ids data in pairs of interactions

How can I then rearrange this data in reading, or even how to write them ?? In python or another language?

    
asked by anonymous 23.11.2017 / 19:46

1 answer

1

A bit confusing question, and having pasted the code instead of printing would also help ... Anyway, if I understood correctly you want each ID to be replaced by a corresponding number, incrementing one unit with each new ID. I made a function which takes as an argument a list and returns it this way.Segue:

def muda_ids(lista):
    antigos_ids = []
    for linha in lista:
        ids = linha.split(',')
        for id in ids:
            if id not in antigos_ids:
                antigos_ids.append(id)
    for cont in range(len(antigos_ids)):
        lista = [w.replace(antigos_ids[cont], str(cont)) for w in lista]
    return lista

For example, passing the following list as an argument:

['100,110', '100,200', '300,154', '400,156', '100,110']

The function returns:

['0,1', '0,2', '3,4', '5,6', '0,1']
    
24.11.2017 / 02:15