首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:从字符串中提取出独特的句子,并将它们放在与之连接的新列中;

Python:从字符串中提取出独特的句子,并将它们放在与之连接的新列中;
EN

Stack Overflow用户
提问于 2021-06-22 16:15:17
回答 1查看 73关注 0票数 0

下午的花草,

当我试图为一些用户准备数据时,我遇到了一些抽象/的挑战。正如我所建议的,在SQL中很难做到这一点,因为没有明确的模式,我在python中尝试了一些东西,但是没有成功(因为我仍然在学习python)。

问题陈述:

我的SQL查询输出要么是excel,要么是文本文件(这取决于我如何发布它,但两者都可以)。我有一个字段( excel或文本文件中的第四列),它包含一个或多个拒绝原因(参见下面的示例),用逗号分隔。同时,在错误(有时)中使用逗号。

字段示例,无需任何修改

INVOICE_CREATION_FAILED[无效地址信息:公司名称,有限公司:第1行蓝莓街10+City++09301+SK|SK000001111|BLD,发票上的公司Id与系统中注册的公司Id :AL12345678901 ABC1D\DL0000001在第2行,不正确的采购订单:第1行的VTB2R\ADLAVA9 9]

期望输出:

地址信息无效;发票上的公司Id与系统中为代码注册的公司Id不匹配;采购订单不正确

Python代码:

代码语言:javascript
复制
import pandas

excel_data_df = pandas.read_excel('rejections.xlsx')

# print whole sheet data
print(excel_data_df['Invoice_Issues'].tolist())

excel_data_df['Invoice_Issues'].split(":", 1)

输出

代码语言:javascript
复制
INVOICE_CREATION_FAILED[Invalid Address information:

我试过分裂字符串,但它不能正常工作。它删除冒号之后的所有内容,这是可以接受的,因为它是以这种方式编码的,但是,我需要修剪不必要数据的字符串,并且只保留每一行所需的输出。如果子字符串可用,我将非常感谢任何关于如何修剪输出的代码建议,我将只从该字符串中提取所需的内容。

在excel中,我通常会使用错误列表,并在FIND和MATCH中嵌套IFs函数。但我不知道怎么用Python..。

非常感谢,

格雷格

EN

回答 1

Stack Overflow用户

发布于 2021-06-22 17:29:47

这不是最快的方法,但在Python中,速度很少是最重要的。

在这里,我们手动创建一个错误字典以及您希望它们映射到什么,然后迭代Invoice_Issues中的值,并使用字典查看该键是否存在。

代码语言:javascript
复制
import pandas

# create a standard dataframe with some errors
excel_data_df = pandas.DataFrame({
    'Invoice_Issues': [
        'INVOICE_CREATION_FAILED[Invalid Address information: Company Name, Limited: Blueberry Street 10+City++09301+SK|SK000001111|BLD at line 1 , Company Id on the Invoice does not match with the Company Id registered for the Code in System: [AL12345678901]|ABC1D|DL0000001 at line 2 , Incorrect Purchase order: VTB2R|ADLAVA9 at line 1 ]']
})

# build a dictionary 
# (just like "words" to their "meanings", this maps 
#  "error keys" to their "error descriptions".
errors_dictionary = {
    'E01': 'Invalid Address information',
    'E02': 'Incorrect Purchase order',
    'E03': 'Invalid VAT ID',
    # ....
    'E39': 'No tax line available'
}


def extract_errors(invoice_issue):
    # using the entry in the Invoice_Issues column, 
    # then loop over the dictionary to see if this error is in there.
    # If so, add it to the list_of_errors.
    list_of_errors = []
    for error_number, error_description in errors_dictionary.items():
        if error_description in invoice_issue:
            list_of_errors.append(error_description)
    return ';'.join(list_of_errors)
    

# print whole sheet data
print(excel_data_df['Invoice_Issues'].tolist())


# for every row in the Invoice_Isses column, run the extract_errors function, and store the value in the 'Errors' column.
excel_data_df['Errors'] = excel_data_df['Invoice_Issues'].apply(extract_errors)

# display the dataframe with the extracted errors
print(excel_data_df.to_string())
excel_data_df.to_excel('extracted_errors.xlsx)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68087219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档