简体   繁体   English

在 python 中使用正则表达式删除部分字符串

[英]Remove part of a string using regex in python

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:我正在尝试从信用卡对帐单的交易 csv 中清理一些信息,所有交易名称的格式如下:

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.交易名称都有多个空格,最后是 state 缩写,每次空格的数量都不同,因为它用于使所有 state 缩写排列。 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.在事务 class 中,我有一个清理名称的方法, transaction.cleanTransaction() ,我想在其中清理名称。

Here is the transaction class currently:这是当前的交易 class:

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 :)

To place only 1 space between each word:要在每个单词之间仅放置 1 个空格:

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 . s.split()返回s中的单词列表。

" ".join() is then used to join all the words in the list into a string with one space between each word. " ".join()然后用于将列表中的所有单词连接成一个字符串,每个单词之间有一个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM