我要删除美元符号格式$###,##0.00
,并用#,##0.00
数字格式替换。
我尝试过更改工作表中一个单元格的格式,但是我不知道它是否在做任何事情,因为文件中没有任何变化。
wb = openpyxl.load_workbook('example.xlsx')
sheet = wb['example']
sheet('E7').number_format = '#,##0.00'
我希望输出将E7的值从 $ 380.00 更改为 380.00 ,但是我收到TypeError: 'Worksheet' object is not callable
且文件没有任何作用。< / p>
答案 0 :(得分:0)
对于单个单元格:
import re
f_clean = lambda x: re.sub('[$\-,\|]', '', x)
f_float = lambda x: float(x) if x!='' else np.NaN
print(f_float(f_clean('$10,000.00')))
# 10000.0
对于整列:
# Here's a solution I use for large datasets:
import re
def lookup_money(s):
"""
This is an extremely fast approach to parsing currency to floats.
For large data, the same currencies are often repeated. Rather than
re-parse these, we store all unique values, parse them, and
use a lookup to convert all figures.
(Should be 10X faster than without lookup dict)
"""
f_clean = lambda x: re.sub('[$\-,\|]', '', x)
f_float = lambda x: float(x) if x!='' else np.NaN
currencies = {curr:f_float(f_convert(curr)) for curr in s.unique()}
return s.map(currencies)
# apply to some df as such:
# df['curr'] = df['curr'].apply(lookup_money)