0

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 :)
1
  • What is the actual format of the source file amexActivityJune.csv and how should it look like after the clean up? Commented Aug 5, 2022 at 10:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.