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 ...
随机推荐
- 你今天Python了吗?(下)
在体验了wxPython的强大之后,让我们把注意力集中到Twisted上来.在C++的世界里,你会发现一个很棒的网络应用框架,那就是ACE了:在Python的地盘,Twisted则是在网络应用框架中当 ...
- Blogilo:Ubuntu下“wlw”
原文首发:http://www.ido321.com/1274.html 这一篇博客是在Ubuntu的博客客户端blogilo下发布的,感觉有点高大上,特此发布一篇博文记录一下. 博客发布在 ...
- PHP图像操作:3D图、缩放、旋转、裁剪、添加水印(三)
来源:http://www.ido321.com/887.html 5.PHP对图像的旋转 1: <div> 2: <h4>旋转之前</h4> 3: <img ...
- kobo boot scripts
#!/bin/sh pkill nickel eink_enable_autoupdate rm -rf /debian/tmp/* /debian/tmp/.* 2>/dev/null mou ...
- Emmet:HTML/CSS代码快速编写神器(转)
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- JAVA中的异常(异常处理流程、异常处理的缺陷)
异常处理流程 1)首先由try{...}catch(Exception e){ System.out.println(e); e.printStackTrace(); }finally{...}结构 ...
- BootStrap入门教程 (二) :BASE CSS(排版(Typography),表格(Table),表单(Forms),按钮(Buttons))
上讲回顾:Bootstrap的手脚架(Scaffolding)提供了固定(fixed)和流式(fluid)两种布局,它同时建立了一个宽达940px和12列的格网系统. 基于手脚架(Scaffoldin ...
- hadoop2.5发布:最新编译 32位、64位安装、源码包、API以及新特性
hadoop2.5发布:最新编译 32位.64位安装.源码包.API以及新特性 http://www.aboutyun.com/thread-8751-1-1.html (出处: about云开发) ...
- java console ( mac osx ) 命令行编码
方法 vi ~/.bash_profile #添加新行, UTF-8表示你平台的编码方式 #例如你是GBK.GB18030的 #替换成你平台console可现实字符编码即可 export JAVA_T ...
- ocp 1Z0-043 61-130题解析
61. You are working in an online transaction processing (OLTP) environment. You realize that the sal ...