【问题标题】:Extract only uppercase text from db field仅从 db 字段中提取大写文本
【发布时间】:2012-02-13 17:15:54
【问题描述】:

我“继承”了一个带有表中字段的数据库,其中小写和大写混合在一起,例如。

gateway 71, HOWARD BLVD, Chispa, NY

它们不容易被代码“拆分”,因为它们并不总是以这种形式出现。我需要的是一种只提取大写字母的方法。这可以用 SQLite 实现吗?

【问题讨论】:

    标签: database sqlite text-extraction


    【解决方案1】:

    在这种情况下,使用您可能有的任何其他 WHERE 要求进行 SELECT 可能会更容易(也许更快),并创建一个游标来迭代结果,在代码中进行大写检查。

    SQLite 的另一个选项是创建一个custom function,因此您可以执行以下操作:

    SELECT foo WHERE MYISALLUPPERFUNC(foo) = 1;
    

    【讨论】:

      【解决方案2】:

      作为NuSkooler mentions,使用游标可能更容易、更快捷;如果您只需要这样做一次,这是一个特别有吸引力的选择。

      这是一个简单的示例(使用 Python REPL 中的内置 SQLite):

      import sqlite3
      
      with sqlite3.connect(":memory:") as conn:
          conn.execute('''create table t (c, newc);''')
          conn.commit()
          conn.execute('''insert into t (c) values (?);''', ('testing MAIN ST',))
          conn.commit() 
          results = conn.execute('select c from t;').fetchall()
          for line in results:
              tokens = line[0].split()
              filtered_tokens = [i for i in tokens if i.isupper()]
              newc = ' '.join(filtered_tokens)
              conn.execute('update t set newc = ?;',(newc,))
              conn.commit()
      
          conn.execute('''select c,newc from t;''').fetchone()
          # (u'testing MAIN ST', u'MAIN ST')
      

      【讨论】:

      • 我认为他想提取大写单词而不是将它们全部变成大写。
      猜你喜欢
      • 1970-01-01
      • 2016-10-27
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多