首页 > 解决方案 > 从单元格中提取特定的文本字符串

问题描述

请看下面的文字:

Mr. Manimaran R MMTFD NL20824448862,Room no-206

我有大约一百万个这样的单元格,我想使用公式从该文本中提取 ID 号。我面临的问题是一些 id 以 NL 开头,一些以 NH 开头,一些以 IN/IWA 开头,并且包含 id 的文本字符串的长度也不同(13/14/15 等)。有什么公式可以让我提取这些id吗?

目前我正在使用这个公式 =MID(text,FIND("NH",text,1),15)。但是我每次都需要为 NL/IN 以及 14/15 等更改它。

标签: excel-formulaexcel-2010

解决方案


您可以使用带有 python3 的 pandas 轻松快速地完成此类任务。

在 python3 外壳中

import pandas as pd
df= pd.read_excel('./your_excel_file.xlsx') ##import from excel
df['ID']=df['Data_header_name'].str.split(' ',expand=True)[4].str.split(',',expand=True)[0] ##here you can use any relevant filters to get what you want
df.to_excel('output_excel.xlsx') ## save to excel

您还需要使用 pip 安装 openpyxl、xlrd、numpy、pandas

pip install openpyxl xlrd numpy pandas

推荐阅读