简体   繁体   中英

Split alphanumeric characters in excel using python

I have a excel sheet containing lot of employee ids as shown below, the data contains only in 1st column

ALopp190 
ARaga789 
Lshastri921
DPatel592 
ANaidu026

When i run the python code i need output as shown below.

ALopp190     k190   190
ARaga789     k789   789
Lshastri921  k921   921
DPatel592    k592   592
ANaidu026    k026   026

Basically it has to split letters and digits and in 2nd column it should always start with k then corresponding number, While in 3rd Column i need only number. This is what i tried and i am not getting the desired output.

import xlrd 
loc = ("C:\\Users\\Kiran\\Desktop\\Emplyoees\\User.xlsx") 
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0) 

import re 
df = "User.xlsx"
res = [re.findall(r'(\w+?)(\d+)', df)[0] ] 

Can you please guide me in fixing this issue.

Using pandas.read_excel

import pandas as pd

df = pd.read_excel('data.xlsx', index_col=None, header=None)
df.columns = ['id']
df['X'] = 'k' + df['id'].str.extract('(\d+)')
df['Y'] = df['id'].str.extract('(\d+)')

df.to_excel('result.xlsx', index=False, header=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM