python-3.x - 如何附加更改数字格式的文本?
问题描述
我从 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.
我该怎么做?
解决方案
我不知道您所有的输入参数,但这适用于您提供的参数。
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
推荐阅读
- python - 无法在 django 中创建超级用户
- reactjs - Create React App 中的 setupProxy.js 不起作用
- python - 从 MongoDb 中的多个数据库中获取数据
- angular - 使用 WebStorm 在 Angular 中向下移动按钮
- ruby - 本地启动 GitHub 页面 Jekyll 页面时出错
- r - R计算两个开始日期和结束日期之间的重叠月份
- xcode - XCode Instruments 无法打开 .trace 文件:无法打开该文件,因为没有这样的文件
- caching - 如何最大化该程序的数据缓存未命中数?
- entity-framework - 实体模型上的自定义吸气剂?
- python - 在特定目录中创建文件