比较db和csv文件中的字符串的拼写

时间:2018-06-21 15:41:43

标签: python sql-server

我正在从CSV文件映射表,并将某些值与数据库中的键进行比较,以获取另一个值。

人们编写CSV文件时可能会出现拼写错误,因此有时在数据库中找不到某些值。

例如人写道:“ ContributionsOther”,数据库键为“ ContributionsOther”

我所做的是删除所有空格和破折号,并从CSV中将值都小写,并在从db创建表时转换了值。以下是以下方法:

def get_trade_type_mappings(self):
    sql = """
    SELECT Code, TradeTypeID
    FROM dbo.TradeType"""

    with self.job.connect(database='rap') as conn:
        trade_types = etl.fromdb(conn, sql)
        trade_types.convert('Code', lambda x: x.replace(' ', '').replace('-', '').lower())
        return dict(trade_types)

def fetch_trade_type_id(self, trade_type):
    # Prevents case and space difference causing issues
    trade_type = trade_type.replace(' ', '').replace('-', '').lower()
    if trade_type == 'cover':
        trade_type = 'covershort'
    elif trade_type == 'short':
        trade_type = 'sellshort'

    return self.get_trade_type_mappings.get(trade_type)

我正在尝试考虑其他可能容易出错的情况。

我写的内容适用于: “其他贡献”与“其他贡献” 但不适用于: “其他贡献”与“其他贡献”

您认为还有什么用吗?我已经看到了一个Levenshtein距离方法,可以比较两个单词之间的拼写...也许我可以将其整合。

0 个答案:

没有答案
相关问题