温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python脚本语言作为转换数据的工具示例分析

发布时间:2021-10-14 14:27:54 来源:亿速云 阅读:104 作者:柒染 栏目:编程语言

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)

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI