Im quite new to python and struggling on this. Any help would be much appreciated please
I have a dictionary as follows:
teams -> {'42': 'arsenal', '48': 'chelsea', '43': 'Liverpool', '49': 'Man City', '24': 'Wolves'}
I have a a file of rows which one i split into a list looks like below:
['Hex', 'Team', 'League', 'Won League']
['0xDEFK', 'arsenal', 'EPL', 'Yes']
['0x1E56', 'chelsea', 'EPL', 'Yes']
['0xFDC0', 'liverpool', 'EPL', 'No']
['0x76YJ', 'Man City', 'EPL', 'Yes']
['0xAB7J', 'Wolves', 'EPL', 'No']
I want to be able to loop through above rows and update the second column "Team" with the number of that team from the "teams" dictionary
ie
EXAMPLE 1:
['0xDEFK', 'arsenal', 'EPL', 'Yes']
WOULD BECOME 42 because key for arsenal is 42:
['0xDEFK', '42', 'EPL', 'Yes']
EXAMPLE 2:
['0x1E56', 'chelsea', 'EPL', 'Yes']
WOULD BECOME 48 because key for chelsea is 48:
['0x1E56', '48', 'EPL', 'Yes']
I've something like below but not having much luck, what am i doing wrong? Is there a easier way to do this? Thanks in advance:
I have a dictionary as follows:
teams -> {'42': 'arsenal', '48': 'chelsea', '43': 'Liverpool', '49': 'Man City', '24': 'Wolves'}
I have a a file of rows which one i split into a list looks like below:
['Hex', 'Team', 'League', 'Won League']
['0xDEFK', 'arsenal', 'EPL', 'Yes']
['0x1E56', 'chelsea', 'EPL', 'Yes']
['0xFDC0', 'liverpool', 'EPL', 'No']
['0x76YJ', 'Man City', 'EPL', 'Yes']
['0xAB7J', 'Wolves', 'EPL', 'No']
I want to be able to loop through above rows and update the second column "Team" with the number of that team from the "teams" dictionary
ie
EXAMPLE 1:
['0xDEFK', 'arsenal', 'EPL', 'Yes']
WOULD BECOME 42 because key for arsenal is 42:
['0xDEFK', '42', 'EPL', 'Yes']
EXAMPLE 2:
['0x1E56', 'chelsea', 'EPL', 'Yes']
WOULD BECOME 48 because key for chelsea is 48:
['0x1E56', '48', 'EPL', 'Yes']
I've something like below but not having much luck, what am i doing wrong? Is there a easier way to do this? Thanks in advance:
with open('info_output.txt', 'r') as info_outputfile: for line in info_outputfile: l = line.split('|') #info = [Hex, Team, League, WonLeague] newlist = [] for element in info: newlist.append ((element[0], dictionary [element[1]], element[4], element[3])) print newlistThe error i get when i run this script is:
Error:Traceback (most recent call last):
element[1] = dictionary[ element[1]]
KeyError: '0'