Reputation: 27
I am trying to clean up some info in a csv of transactions from a credit card statement, all of the transaction names are formatted as follows:
AplPay SUBWAY SCOTTSDALE AZ
POPEYES 8703 0000 TEMPE AZ
SALAD AND GO #1138 0PHOENIX AZ
The transactions names all have multiple spaces followed by a state abbreviation at the end, the number of spaces is different each time as it is used to make all of the state abbreviations line up. Would using a regex pattern to remove these be the correct course or is there a better option?
Currently I am building a list of the transactions from this specific credit card:
AmextransList = []
fileName = 'amexActivityJune.csv'
cnt = 0
with open(fileName, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
if cnt != 0:
AmextransList.append(transaction(row[1], row[0], row[2]))
cnt += 1
In the transaction class I have a method to clean the name, transaction.cleanTransaction() where I would like to do the cleaning of the names.
Here is the transaction class currently:
class transaction:
name = ""
date = ""
amount = ""
def __init__(self, n, d, a):
self.name = n
self.date = d
self.amount = a
def printTransaction(self):
print("Name: ", self.name, " Amount: ", self.amount, " Date: ", self.date)
def cleanTransaction(self): #This is where I need help
#Remove the ______________AZ from the name
#More work not for StackOverFlow :)
Upvotes: 0
Views: 60
Reputation: 1935
To place only 1 space between each word :
s = 'SALAD AND GO #1138 0PHOENIX AZ'
clean = " ".join(s.split())
print(clean) # SALAD AND GO #1138 0PHOENIX AZ
s.split()
returns a list of words in s
.
" ".join()
is then used to join all the words in the list into a string with one space between each word.
Upvotes: 1