python怎么可以让Excel表里左边的数据实现效果为右边数据?让带—号的数据可以自动拆解排列。
可以用pandas处理,这里是将处理后的数据保存新文件中,不知道你具体的需求是什么样的
参考示例
import pandas as pd
from pandas import DataFrame
def split_dash_data(excel_path: str, output_path: str):
# 读取Excel文件,headers为None时无表头,数据区从第一行开始
sheet = pd.read_excel(excel_path, sheet_name='Sheet1', header=None)
new_data = []
for row in sheet.index.values:
item = sheet.iloc[row, 0]
if '-' in item:
item = item.strip().replace('号', '')
start, end = item.split('-')
for num in range(int(start), int(end) + 1):
new_data.append(str(num) + '号')
new_df = DataFrame(new_data)
# 写入到新的Excel文件中
new_df.to_excel(output_path, sheet_name='Sheet1', header=None, index=None)
if __name__ == '__main__':
root_path = '/Users/jason315/Desktop'
split_dash_data(f'{root_path}/data.xlsx', f'{root_path}/new_data.xlsx')