首页 > 解决方案 > 如何附加更改数字格式的文本?

问题描述

我从 HTML 中获取数字,其中一些是 %、4 位和 7 位(37.89%、3.464、2,193.813)。我想只保存数字,而不是百分比,没有千位分隔符(“。”)。

        list_of_rows = []
        for row in table.findAll('div', attrs={'class': 'quadrado'}):
            list_of_cells = []
            for cell in row.findAll('span', attrs={'class': 'circulo'}):
                text = cell.text
                # print(text)
                for cell_index in row.findAll('span', attrs={'class': 'triangulo'}):
                    text_index = cell_index.text
                    list_of_cells_index = [text, text_index] 
                    list_of_cells_index_clean = ','.join(list_of_cells_index) # remove brackets and ''
                    # print(list_of_cells_index_clean) 
                list_of_cells.append(list_of_cells_index_clean)
            list_of_rows.append(list_of_cells)

    outfile = open("./list.csv", "a") 
    writer = csv.writer(outfile, lineterminator = '\n')
    writer.writerows(list_of_rows)

我想得到: 37.89%, 3464, 2193,813. 我该怎么做?

标签: python-3.xcsvweb-scrapingappendnumber-formatting

解决方案


我不知道您所有的输入参数,但这适用于您提供的参数。

s = ('37.89%', '3.464', '2,193.813')

for item in s:
  remove_comma = item.replace(',', '')
  keep_percentage = re.findall(r'\d{1,4}\.\d{1,4}%', remove_comma)
  if keep_percentage:
     keep_percentage = ''.join(keep_percentage)
     print (keep_percentage)
  else:
    if (len(remove_comma)) == 5:
        print (remove_comma.replace('.', ''))
    else:
        print (remove_comma.replace('.', ','))

**OUTPUTS**
37.89%
3464
2193,813

推荐阅读