I'm fairly new to python and I have a task where I have to import some addresses from a text file and then search for and abbreveate the title for the road (i.e change 'road' to 'RD').
So far I've managed to import the file and I've been able to form a 2D array where the whole address is in a seperate array inside the main array. I've been looking for a way to get inside the arrays so I can split the strings them so I can do the abbreveation and then output each sub array on its own line in excel.
This is my code at the moment:
def sec_2():
addresses = []
with open('Addresses2.txt', newline='') as Addresses2:
for row in csv.reader(Addresses2):
addresses.append(row)
print(addresses)
The code outputs this so far:
[['52 Corinthian Road', ' First Floor'], ['20 Ingram Street', ' Forest Hills', ' New York'], ['14 Westbourne Terrace Road', ' Buxton'], ['The Terrace Restaurant', ' 81 Royal Street', ' Solihull']]
I need it to be:
[['52', 'Corinthian', 'Road', ' First', 'Floor']], [['20', 'Ingram', 'Street', ' Forest', 'Hills', 'New', 'York'] etc...
addresses.append(row.split())instead of trying to split the rows after the fact.[['52', 'Corinthian', 'Road', ' First', 'Floor']]is this your requirement or a typo? Doesn't really seem to serve any purpose. Or do you mean to say[['52', 'Corinthian', 'Road', ' First', 'Floor'],['20', 'Ingram', 'Street', ' Forest', 'Hills', 'New', 'York']]