python读取文本、配对、插入数据脚本
#在工作中遇见了一个处理数据的问题,纠结了很久,写下记录一下。#-*- coding:UTF-8 -*-
#-*- author:ytxu -*-
import codecs, os, sys, platform, string
def env():
return platform.system()
def read_file(uri, charset = "utf-8"):
f = codecs.open(uri, "r", charset)
s = f.read()
f.close()
return s
def write_file(uri, content = u"", charset = "utf-8"):
f = codecs.open(uri, "w", charset)
f.write(content)
f.close()
def parse(f, osep, ls):
ctx = read_file(f)
r = []
for l in ctx.split(osep):
tl = []
for c in l.split(ls):
c = c.strip()
len(c) > 0 and tl.append(c)
r.append(tuple(tl))
return r
def parse_log(f, osep, ls):
ctx = read_file(f)
r = []
for l in ctx.split(osep):
tl = []
for c in l.split(ls):
c = c.strip()
tl.append(c)
r.append(tuple(tl))
return r
def found_t(ts, n):
for t in ts:
if t[0].rfind(n) != -1:
return t
def found_id(us, n):
for u in us:
if (len(u) < 2):
break
if (u[1].rfind(n) != -1) or (u[2].rfind(n) != -1) or (u[3].rfind(n) != -1):
return u[0]
return None
if __name__ == '__main__':
env() == "Windows" and os.system("cls")
rst = []
us = parse_log("./user.txt", '\n', '\t')
ts = parse("./teacher.txt", '\r\n', ' ')
for s in parse("./student.txt", '\r\n', ' '):
t = found_t(ts, s[5])
if t is None:
print s[5]
continue
sid = found_id(us, s[4])
tid = found_id(us, t[2])
if sid is not None and tid is not None:
q = u"INSERT into student_teacher_relation(student_id,teacher_id,subject) values (%d,%d,'%s')" %(int(sid),int(tid),t[1])
# print sid, tid, q
rst.append(q)
# print u"学生帐号:", s[4], u"学生名称:", s[0], u"代课老师:", t[0], u"代课老师的帐号:", t[2]
write_file("./insert.sql", string.join(rst, "\r\n"))
python读取文本、配对、插入数据脚本的更多相关文章
- Python实现随机读取文本N行数据
工作中需要判断某个文本中的URL是否能正常访问,并且随机获取其中N行能正常访问的URL数据,我的思路是:读取文本每一行数据,用urlopen访问,将返回状态码为200的URL保存到一个列表,获得列表长 ...
- Python向mysql数据库插入数据
一.向表tcolor中插入数据的主要流程如下: import datetimeimport pymysql.cursorsconnection = pymysql.connect(host='loca ...
- Python读取文本,输出指定中文(字符串)
因业务需求,需要提取文本中带有检查字样的每一行. 样本如下: 1 投入10kVB.C母分段820闭锁备自投压板 2 退出10kVB.C母分段820备投跳803压板 3 退出10kVB.C母分段820备 ...
- python读取数据库并把数据写入本地文件
一,介绍 上周用jmeter做性能测试时,接口B传入的参数需要依赖接口A生成的借贷申请ID,接口A运行完需要把生成的借贷申请ID导出来到一个文件,作为参数传给接口B,刚开始的时候,手动去数据库倒, 倒 ...
- MySQL高级知识(十)——批量插入数据脚本
前言:使用脚本进行大数据量的批量插入,对特定情况下测试数据集的建立非常有用. 0.准备 #1.创建tb_dept_bigdata(部门表). create table tb_dept_bigdata( ...
- python读取grib grib2气象数据
如何读取GRIB数据?快看Python大神整理的干货! 橙子心法 百家号17-11-0116:30 GRIB是WMO开发的一种用于交换和存储规则分布数据的二进制文件格式,主要用来表示数值天气预报的产品 ...
- 两分钟解决Python读取matlab的.mat数据
Matlab是学术界非常受欢迎的科学计算平台,matlab提供强大的数据计算以及仿真功能.在Matlab中数据集通常保存为.mat格式.那么如果我们想要在Python中加载.mat数据应该怎么办呢?所 ...
- 使用python读取文本中结构化数据
需求 read some .txt file in dir and find min and max num in file. solution: echo *.txt > file.name ...
- python读取文本数据某一列
import codecs f = codecs.open('test1 - 副本.txt', mode='r', encoding='utf-8') # 打开txt文件,以'utf-8'编码读取 l ...
随机推荐
- UNDO表空间设置
flashback query和flashback table都是以用UNDO表空间的内容来进行恢复数据 查看undo内容保存的时间: SQL> show parameter undo_re N ...
- 将spfile存储在ASM中
数据库的spfile开始是存储在普通的文件系统中,如下所示: SQL> show parameter spfile NAME TYPE VALUE ----------------------- ...
- 未能加载Connector/NET :: v6.7.4
//从*.config文件获取连接字符串和提供程序 string dp = ConfigurationManager.AppSettings["provider"]; string ...
- 7-wonders-in-java
https://code.google.com/p/7-wonders-in-java/
- ocp 1Z0-047 61-130题解析
61. Evaluate the following SQL statements that are issued in the given order:CREATE TABLE emp(emp_no ...
- HDU 5810 Balls and Boxes (找规律)
Balls and Boxes 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5810 Description Mr. Chopsticks is i ...
- Linux下MongoDB备份脚本
#!/bin/bash today=`date +%Y%m%d` mongodump -h localhost -d salary -o /home/chzhao/mongobackup/$today ...
- STL学习系列三:Deque容器
1.Deque简介 deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. deque在接口上和vector非常 ...
- opencv 彩色图像亮度、对比度调节 直方图均衡化
直接上代码: #include <Windows.h> #include <iostream>// for stand I/O #include <string> ...
- HDU1028Ignatius and the Princess III(母函数)
http://acm.hdu.edu.cn/showproblem.php?pid=1028 母函数: 例1:若有1克.2克.3克.4克的砝码各一 枚,能称出哪几种重量?各有几种可能方案? 如何解决这 ...