使用pythonparsingExcel中的数据

在Excel中,我必须将下面的值从一个单元格分成两个:

2016-12-12 (r=0.1) 2016-12-13* (r=0.7) 

如何在Python中这样做,以便在Excel文件中,date和“r =#”将在不同的单元格中? 还有,有办法自动删除“*”的标志?

如果你使用pandas,这个任务是非常简单的:

build立一个testing文件:

 import pandas as pd df_out = pd.DataFrame( ['2016-12-12 (r=0.1)', '2016-12-13* (r=0.7)'], columns=['data']) df_out.to_excel('test.xlsx') 

代码转换string:

 def convert_date(row): return pd.Series([c.strip('*').strip('(').strip(')') for c in row.split()]) 

testing代码:

 # read in test file df_in = pd.read_excel('test.xlsx') print(df_in) # build a new dataframe df_new = df_in['data'].apply(convert_date) df_new.columns = ['date', 'r'] print(df_new) # save the dataframe df_new.to_excel('test2.xlsx') 

结果:

  data 0 2016-12-12 (r=0.1) 1 2016-12-13* (r=0.7) date r 0 2016-12-12 r=0.1 1 2016-12-13 r=0.7