In [23]:
fu = open('users-london.csv', 'r')

In [24]:
fout = open('users-london-renum.txt', 'w')

In [25]:
idmap = {}

In [26]:
newid = 0

In [27]:
for line in fu:
    uid, uname = map(int, line[:-1].split(','))
    idmap[uid] = newid
    fout.write('%d,%d\n' % (newid, newid))
    newid += 1

In [28]:
fu.close()
fout.close()

# Filter friendships present in SF, write only renumbered friendship relations


In [13]:
ffr = open('FoursquareFriendship.csv', 'r')

In [14]:
ffr.readline() # Skip header


Out[14]:
'user1,user2\r\n'

In [15]:
ffrnew = open('FoursquareFriendship-london.csv', 'w')

In [16]:
org_uids = set(idmap.keys())
for line in ffr:
    u1, u2 = map(int, line[:-2].split(','))
    if u1 in org_uids and u2 in org_uids:
        ffrnew.write('%d,%d\n' % (idmap[u1], idmap[u2]))

In [17]:
ffr.close()
ffrnew.close()

# Rewrite cascades with renumbered user IDs


In [18]:
fcheck = open('FoursquareCheckins20110101-20111231_near_london.csv', 'r')

In [19]:
fcheck_new = open('london/FoursquareCheckins20110101-20111231_near_london-renum.csv', 'w')

In [20]:
fcheck_new.write(fcheck.readline()) # Rewrite header

In [21]:
for line in fcheck:
    us, lat, lng, tm, loc = line[:-1].split(',')
    fcheck_new.write('%d,%s,%s,%s,%s\n' % (idmap[int(us)], lat, lng, tm, loc))

In [22]:
fcheck.close()
fcheck_new.close()

In [ ]: