【问题标题】:I need to import data from .txt to SQL我需要将数据从 .txt 导入 SQL
【发布时间】:2015-12-28 20:46:17
【问题描述】:

我有一个需要导入数据库的文本文件,可能使用 SQL 来执行此操作。我只是想将地址按以下格式放入表格中:

地址城市、州、邮编。

以下是我尝试使用的文本的更改示例:

DOE, JANE
     id:0123456465       alt id:246465  165165  department:TEST    

*Address Information--  Mailing Address:1

      address1:
          City, State:TEST CITY, STATE
                Line1:14566 Test Avenue
                  Zip:12345
      address2:
  none

      address3:
  none

我只需要地址 1 中的信息,但有 40,000 个条目。有没有人有办法让我有效地完成这项工作?

【问题讨论】:

  • 您心目中的 RDBMS 是否有批量加载实用程序?
  • 1.) 这些条目是否在一个大文件中? 2.) 您是否可以访问诸如 python 或 awk 之类的脚本语言(如果您使用 Linux,那么您有。)
  • @tzunghaor 是的,它在一个大文件中,我可以访问 python

标签: sql import text-files


【解决方案1】:

作为一个python实现的例子,

import re
idPat = re.compile("id:(\d+)")
cityPat = re.compile("City, State:(.*)")
zipPat = re.compile("Zip:(\d{5})")
with open('data.txt', 'r') as f:
    data = f.read().split('\n') # or split('\r\n') in windows

ID = CITY_STATE = ZIP = None

# assuming you have all of the data in an array, with each element being a line
foreach line in data: 
    if ID is None:
        m = idPat.search(line)
        if m is not None:
            ID = m.group(0)
            CITY_STATE = ZIP = None
    else:
        if CITY_STATE is None:
            m = cityPat.search(line)
            if m is not None:
                CITY_STATE = m.group(0)
                ZIP = None
        elif ZIP is None:
            m = zipPat.search(line)
            if m is not None:
                ZIP = m.group(0)
                print "UPDATE `table` SET `CityState`='%s', `Zip`='%s' WHERE `id`='%';"%(CITY_STATE, ZIP, ID)
                ID = CITY_STATE = ZIP = None

这只会打印出一堆 sql 语句。您也许可以将其批量导入到您的 phpmyadmin(例如)控制台中。或者 python 具有连接到许多数据库类型的绑定。

【讨论】:

    【解决方案2】:

    如果结构总是这么严格,并且值只有 ASCII 字符,那么这样的东西会生成一个 sql 文件

    fin = open('janedoe.txt', 'r')
    fout = open('janedoe.sql', 'w')
    
    inAddress1 = 0
    address = {}
    dataNo = 0 # just want to see how much is processed
    for line in fin:
        line = line.strip()
        if inAddress1:
            if line == 'address2:':
                fout.write( "insert into Address (CityState, Line1, Zip) " + 
                    "values ('%s', '%s', '%s');\n" % 
                    (address['CityState'], address['Line1'], address['Zip']))
                inAddress1 = 0
            else:
                key, value = line.split(':', 1)
                key = key.translate(None, ', ')
                value = value.replace("'", "''") # sql escape string
                address[key] = value
        elif line == 'address1:':
            dataNo += 1
            if 0 == dataNo % 100:
                print dataNo
    
            inAddress1 = 1
            address = {}
    
    fin.close()
    fout.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 2016-03-01
      相关资源
      最近更新 更多