我有以下列表,我想转换成一个熊猫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]
我正在使用漂亮的汤来提取上面提到的列表
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)
这段代码基本上获取了之间的所有文本。有什么办法可以在提取时去掉逗号吗?
发布于 2020-01-04 12:11:09
如果您的列表是一个字符串,这可能会起作用
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)
但是,避免使用异常的最好方法是以正确的方式读取列表,使用适当的分隔符,并替换所有逗号
https://stackoverflow.com/questions/59587737
复制相似问题