【问题标题】:Read a text file and transfer contents to mysql database table using python使用python读取文本文件并将内容传输到mysql数据库表
【发布时间】:2012-07-28 17:55:53
【问题描述】:

我是使用 python 编程处理数据库的新手。
通过使用 python 编程,我想读取由 STUDEN T_NAME、STUDENT_MARKS 组成的原始文本文件。由管道符号分隔(如下例所示),我想将此数据推送到学生表中,该表由 2 列(STUDENT_NAME,STUDENT_MARKS)和各自的数据值组成。

输入数据文件会是这样的(它由数千条这样的记录组成),我的输入文件是.Dat文件,它只以记录开头,每行包含0或更多的记录(没有固定的每行的记录数),没有其他关键字出现在其他任何地方::

记录 STUDENT_NAME|杰克| STUDENT_MARKS|200|学生姓名|克拉克 |学生成绩|200|学生姓名|艾基尔 | STUDENT_MARKS|30| 学生姓名|水库 | STUDENT_MARKS|200|学生姓名|琼斯 | STUDENT_MARKS|200|学生姓名|杰克 | STUDENT_MARKS|100|

输出mysql表table::

学生姓名| STUDENT_MARKS

 jack   |   200
 clark  |   200

.......

请建议我以有效的方式读取文件和推送数据。 如果有人能给我脚本来实现这一点,我将不胜感激。

【问题讨论】:

  • 还准确描述输入文件中的内容。它是否以records 开头?该关键字或任何其他关键字是否出现在其他任何地方?每一行是否包含一个学生记录,或者是否有一长行包含数千条记录?
  • 嗨 gauden,我的输入文件是 .Dat 文件,它只以记录开头,每行包含 0 或多个记录(每行没有固定的记录数),没有其他关键字出现在其他任何地方,请建议我。

标签: python python-3.x


【解决方案1】:
# import mysql module
import MySQLDB

# import regular expression module
import re

# set file name & location (note we need to create a temporary file because 
# the original one is messed up)

original_fyle = open('/some/directory/some/file.csv', 'r')
ready_fyle = open('/some/directory/some/ready_file.csv', 'w')


# initialize & establish connection 
con = MySQLdb.connect(host="localhost",user="username", passwd="password",db="database_name") 
cur = con.cursor()

# prepare your ready file 

for line in original_fyle:
    # substitute useless information this also creates some formatting for the 
    # actuall loading into mysql
    line = re.sub('STUDENT_NAME|', '\n', line) 
    line = re.sub('STUDENT_MARKS|', '', line)
    ready_fyle.write(line)

# load your ready file into db

# close file
ready_file.close()

# create a query 
query = 'load data local infile "/some/directory/some/ready_file.csv" into table table_name field terminated by "|" lines terminated by "\n" '
# run it 
cur.execute(query)
# commit just in case 
cur.commit()

【讨论】:

    【解决方案2】:

    本着being kind to newcomers 的精神,一些代码可以帮助您入门:

    # assuming your data is exactly as in the original question
    data = '''records STUDENT_NAME| jack | STUDENT_MARKS|200| STUDENT_NAME| clark |STUDENT_MARKS|200| STUDENT_NAME| Ajkir | STUDENT_MARKS|30| STUDENT_NAME| Aqqm | STUDENT_MARKS|200| STUDENT_NAME| jone | STUDENT_MARKS|200| STUDENT_NAME| jake | STUDENT_MARKS|100|'''
    
    data  = data.split('|')
    
    for idx in range(1, len(data), 4):
        # every second item in the list is a name and every fourth is a mark
        name = data[idx].strip() # need to add code to check for duplicate names
        mark = int(data[idx+2].strip()) # this will crash if not a number
        print(name, mark) # use these values to add to the database
    

    您可能想使用SQLite using this tutorial 来学习如何在 Python 中使用此类数据库。 this tutorial about file input 可能有用。

    您可能想从这个开始,然后是come back with some code

    【讨论】:

      猜你喜欢
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 2015-04-26
      • 1970-01-01
      • 2013-05-12
      相关资源
      最近更新 更多