人生的第一个Python程序
来源:互联网 发布:java 红黑树原理 编辑:程序博客网 时间:2024/04/30 14:21
今天因为给BeauBeau提供的抽奖号码做SQL文件,一开始收到ZIP文件解开压缩之后被吓到了——29个CSV文件,每个文件保存了1000个奖券ID和号码-_-!
照上次一样,打开每个CSV文件做先做单独的SQL文件,每个SQL中有1000条插入语句,随后将29个文件的所有SQL语句都复制粘贴到同一个总的SQL文件中。
CSV文件中的结构是“ID,NUMBER”的结构,其中ID是7位数字,NUMBER是11位数字。这样用正则式来进行捕捉的时候就比较方便了,在Eclipse的查找/替换功能中所使用的正则式就是“(/d{7}),(/d{11})”,进行替换的文本内容就是“INSERT INTO cards VALUES ('$1','$2',now());”。使用这种方法对29个CSV文件中的内容进行替换。
接下来,由于我觉得手动复制粘贴这29000条插入语句实在是太痛苦了,所以打算用Python来完成这项工作。这是我第一次自己动手写Python代码,感觉还挺顺利的。
所有代码如下:
import sys, os
def readFile(filename):
file=open(filename, "r")
s=file.read().strip()
file.close()
return s
def writeFile(filename, files):
content=[]
for f in files:
print "reading file ' %s ' " % f
s=readFile(f)
print "read file ' %s ' completed" % f
content.append(s)
print "writing file ' %s ' " % filename
file=open(filename, "w")
file.write("/n/*-----This is a seperating line.-----*//n".join(content))
file.close()
print "write file ' %s ' completed" % filename
filters=['.txt']
fullpath=os.getcwd();
print "opening directory: ' %s ' " % fullpath
sys.path.append(fullpath)
files = os.listdir(fullpath)
files =[f for f in files if os.path.splitext(f)[1].lower() in filters]
writeFile("beaunet_be_card.sql", files)
def readFile(filename):
file=open(filename, "r")
s=file.read().strip()
file.close()
return s
def writeFile(filename, files):
content=[]
for f in files:
print "reading file ' %s ' " % f
s=readFile(f)
print "read file ' %s ' completed" % f
content.append(s)
print "writing file ' %s ' " % filename
file=open(filename, "w")
file.write("/n/*-----This is a seperating line.-----*//n".join(content))
file.close()
print "write file ' %s ' completed" % filename
filters=['.txt']
fullpath=os.getcwd();
print "opening directory: ' %s ' " % fullpath
sys.path.append(fullpath)
files = os.listdir(fullpath)
files =[f for f in files if os.path.splitext(f)[1].lower() in filters]
writeFile("beaunet_be_card.sql", files)
程序的功能很简单,这也是我在Python的道路上迈出的第一步。
有时间的时候重写这段代码,加入正则替换功能。
- 人生的第一个Python程序
- 人生第一个程序!
- 人生第一个程序
- 人生的第一个程序呀~
- 人生的第一个Java程序
- Python的第一个程序
- Python的第一个程序
- python的第一个程序
- 人生第一个scala程序
- 【python】我的第一个python程序
- 我人生中第一个真正的CUDA程序
- 写出自己人生的第一个小程序
- 第一个Python程序
- 第一个Python程序
- 第一个Python程序
- 第一个python 程序
- 第一个Python程序
- 第一个python程序
- asp.net验证控件气泡提示效果源代码
- JSF(JSP)中使用FCKeditor上传图片问题的解决
- aspx 获得提交的表单对象
- 需求管理
- Rational TestManager (TM)安装(转摘)
- 人生的第一个Python程序
- 关于"多态"static方法的理解
- FTP命令详解
- oracle分页查询
- PHP Session丢失问题解决
- ORACLE 中ROWNUM用法总结!(精华)
- 未来
- Differences Among Greedy, Reluctant, and Possessive Quantifiers
- 常用电源IC