新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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)
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。