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 ...
随机推荐
- linux 用 SSH2协议远程连接并控制 linux
[参考链接](http://php.net/manual/zh/ssh2.installation.php) ssh2_exec 并不能打印所有的命令的提示信息 如果有返回的字符串信息,可以打印,或重 ...
- bzoj 3218 a + b Problem(最小割+主席树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3218 [题意] 给n个格子涂白或黑色,白则wi,黑则bi的好看度,若黑格i存在: 1& ...
- 安装CPqD/ofdissector遭遇的错误
为了安装支持openflow1.3的wireshark插件,在下载了ofdissector.git,并进入了其src目录后,执行scons install,出现如下错误: util/FieldMana ...
- 基于Maven管理的Mapreduce程序下载依赖包到LIB目录
1.Mapreduce程序需要打包作为作业提交到Hadoop集群环境运行,但是程序中有相关的依赖包,如果没有一起打包,会出现xxxxClass Not Found . 2.在pom.xml文件< ...
- 轻松突击ThreadLocal
本文出自 代码大湿 代码大湿 ThreadLocal是用来保存线程的本地变量,可以保证每个线程都有一个自己的变量(包括static变量). 本文所有代码请点击我 1 看个实际场景. 我们要设计一个序列 ...
- java工程师的标准
1.技术广度方面至少要精通多门开源技术吧,研究过struts\spring\hibernate等的源码. 2.项目经验方面从头到尾跟过几个大项目,头是指需求阶段,包括需求调研.尾是指上线交付之后,包括 ...
- HTML5每日一练之details展开收缩标签的应用
details标签的出现,为我们带来了更好的用户体验,不必为这种收缩展开的效果再编写JS来实现.注:目前仅Chrome支持此标签. details有一个新增加的子标签——summary,当鼠标点击su ...
- Linux上svn服务器的搭建
安装svn服务器 直接用yum安装,命令如下: #yum install -y subversion 验证是否安装成功. #svnserve --version 创建SVN版本库 在home目录下创建 ...
- Educational Codeforces Round 7 - E. Ants in Leaves
题目链接:http://www.codeforces.com/contest/622/problem/E 题意是给你一棵树,1为根,每个叶子节点有一个蚂蚁,移动到一个邻接节点时间耗费为1,一个节点上不 ...
- 任务分发系统gearman
1 Gearman是什么 Gearman Job Server@http://gearman.org/. Gearman 是一个任务分发系统,它提供了一个分发框架,能够分发某类任务到更适合处理这类任务 ...