python脚本语言作为转换数据的工具示例分析
python脚本语言作为转换数据的工具示例分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
站在用户的角度思考问题,与客户深入沟通,找到芦溪网站设计与芦溪网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都网站建设、企业官网、英文网站、手机端网站、网站推广、空间域名、网络空间、企业邮箱。业务覆盖芦溪地区。
都说python是解释形语言,意思是读取一行执行一行,现在就有一段python程序摆在面前,其功能是“将一个csv文件名以及按文件中的内容转换成多个以内容为目录的linux下目录结构”; 这段话应该比较的模型化,用一个具体的例子来说会容易理解点,这里就假设有这样的一个小需求:现有一个压缩文件 JiangSu-1949-10-01-HumanScience.csv.gz,该压缩文件包含了江苏省1949年10月1日前建立的人文科学专业大学的详细名单,需要转换为一组文件夹,每个文件夹单独存放从签署大学总录中包含的一个大学详细信息。(注:
以下仅为实现该小需求的示例python, 参数文件为 JiangSu-1949-10-01-HumanScience.csv.gz)
if SpecifiedUnivercity is None:
return None
return SpecifiedUnivercity.groups()
def TransformData(TotalUniversities, RootUnivercity_Path, pattern):
SpecifiedUniversity = ResolveUnivercity(TotalUniversities, pattern)
if SpecifiedUniversity is None:
print 'Cannot resolved TotalUniversities file'
return 1
else:
print 'UniversityName: [%s] Born: [%s-%s-%s] Area: [%s]' % SpecifiedUniversity
if not SpecifiedUniversity[4] in ["NatureScience", "HumanScience"]:
print "system only analyze Nature or Human Science"
return 1
if RootUnivercity_Path is None:
RootUnivercity_Path = '%s' % SpecifiedUniversity[0]
gfile = gzip.open(TotalUniversities, "rb")
rownum = 0
HasNext = False
SpecifiedUnivercity_Content = {}
SpecifiedUnivercity_DetailContent = None
for row in csv.reader(gfile):
if rownum == 0:
header = row
else:
SpecifiedUniversity_Name = row[0]
if len(SpecifiedUniversity_Name) > 0:
HasNext = True
if SpecifiedUnivercity_Content.has_key(SpecifiedUniversity_Name):
SpecifiedUnivercity_DetailContent = SpecifiedUnivercity_Content[SpecifiedUniversity_Name][1]
else:
rownum = 0
# create new file
SpecifiedUniversity_File = '%s_%s_%s.csv' % (SpecifiedUniversity[0], SpecifiedUniversity_Name, SpecifiedUniversity[2])
AbsolutePath = os.path.join(RootUnivercity_Path, SpecifiedUniversity_Name)
AbsolutePathName = os.path.join(AbsolutePath, SpecifiedUniversity_File)
if not os.path.exists(AbsolutePath):
os.makedirs(AbsolutePath, 0777)
outfile = open(AbsolutePathName, 'wb')
SpecifiedUnivercity_DetailContent = csv.writer(outfile, Token=',')
# insert into SpecifiedUniversity
SpecifiedUnivercity_Content[SpecifiedUniversity_Name] = [outfile, SpecifiedUnivercity_DetailContent, False]
else:
HasNext = False
if SpecifiedUnivercity_DetailContent is not None:
SpecifiedUnivercity_DetailContent.writerow(row)
else:
print "row %d has no owner file" % rownum
rownum += 1
gfile.close()
for key in SpecifiedUnivercity_Content.keys():
SpecifiedUnivercity_Content[key][1].close()
SpecifiedUnivercity_Content[key][0].close()
if __name__ == "__main__":
#JiangSu-1949-10-01-HumanScience.csv.gz
ResolvedSpecofiedUnivercity = re.compile(r'(/w{2,})-(/d{4})-(/d{2})-(/d{2})-(/w{1,})/.')
for file in sys.argv[1:]:
TransformData(file, None, ResolvedSpecofiedUnivercity)
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
网页标题:python脚本语言作为转换数据的工具示例分析
本文网址:http://hbruida.cn/article/gicosj.html