猿问

如何更改excel电子表格中某些列的数字格式?

我想删除美元符号格式,$###,##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并且文件没有发生任何事情。


慕仙森
浏览 112回答 1
1回答

慕森卡

对于单个单元格:import ref_clean = lambda x: re.sub('[$\-,\|]', '', x)f_float = lambda x: float(x) if x!='' else np.NaNprint(f_float(f_clean('$10,000.00')))# 10000.0对于整个列:# Here's a solution I use for large datasets:import redef 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)
随时随地看视频慕课网APP

相关分类

Python
我要回答