我有以下列表,我想将其转换为 Pandas DataFrame,分成几列。我使用逗号( ,
)将其分成几列。因此,我想仅从金额值中删除逗号( ,
),例如$1,2344.10
。有没有办法使用Python删除它们?或者甚至 RegEx 也可以。
[1, , , 123, , 01/01/2000, S, $1,234.12 , $0.00 , $0.00 , $0.00 , $0.00 , $0.00 , $0.00 , $0.00 , $1,234.12 , $0.00 , $0.00]
我正在使用 beautifulsoup 来提取上面提到的列表
table_details = soup.find('div', class_='k-grid-content k-auto-scrollable')
rows = table_details.find_all('tr')
for td in rows:
row_td = td.find_all('td')
# Remove all HTML tags
str_cells = str(row_td)
text = BeautifulSoup(str_cells, "lxml").get_text()
print(text)
这段代码基本上获取了 .有什么办法可以在提取时去掉逗号吗?
最佳答案
如果您的列表是字符串,这可能会起作用
list = str(list)
for i in range(1,len(list)-1):
if list[i+1].isdigit() and list[i-1].isdigit() and list[i]==',':
list.pop(i)
但是避免处理异常的最佳方法是使用正确的分隔符以正确的方式读取列表,并替换所有逗号
关于python - 使用 Python 从列表中的货币值中删除标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59587737/