您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“python怎么读文件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python怎么读文件”吧!
#!usr/bin/env python # encoding:utf-8 ###删除包含关键字“ Time taken: 0.” def readfile(filename): lines = [line for line in open(filename)] def printTimeTaken(filename): filename="data/aaa.csv" lines = [line for line in open(filename)] for line in lines[: ]: if not line.__contains__("Time taken: 0."): print(line.replace('\n', '')) def appendToFile(context,filepath): print() def contactexp(tb,date_list,cpexp,file,filesql): for tbname in tb: if tbname.replace('\n','').endswith("day") or tbname.replace('\n','').endswith("semester")or tbname.replace('\n','').endswith("7days"): for date_item in date_list: file.write(cpexp.format_map(vars()).lower() + "\n") filesql.write(addPart.format_map(vars()).lower()+"\n") else: date_item='2020-08-01' file.write(cpexp.format_map(vars()).lower() + "\n") filesql.write(addPart.format_map(vars()).lower()+"\n") def whh(filename,file,filesql): with open(file, "w+") as file: with open(filesql, "w+") as filesql: tbname_list=[] lines = [line for line in open(filename)] for line in lines[: ]: if line.lower().replace('\n','').endswith("_"): tbname_list=[line.lower().replace('\n','')+"day",line.lower().replace('\n','')+"week",line.lower().replace('\n','')+"semester",line.lower().replace('\n','')+"7days"] # tbname_list = [line.lower().replace('\n', '') + "week"] contactexp(tbname_list, date_list, cpexp, file,filesql) else: #for date_item in date_list: date_item = '2020-08-01' tbname=line.lower().replace('\n','') file.write(cpexp.format_map(vars()).lower() + "\n") filesql.write(addPart.format_map(vars()).lower() + "\n") #date_list=["2020-06-15","2020-06-16","2020-06-17","2020-06-18","2020-06-19","2020-06-20","2020-06-21"] date_list=["2020-08-01"] cpexp="hadoop fs -rm -r /user/hive/warehouse/dev_treasury.db/{tbname}/day={date_item} \n hadoop distcp hdfs://10.1.11.3:8020/user/hive/warehouse/treasury.db/{tbname}/day={date_item} hdfs://sandbox-hadoop-03.xuele.net:8020/user/hive/warehouse/dev_treasury.db/{tbname}/day={date_item}" addPart="alter table {tbname} add partition (day='{date_item}');" #fileC= open("data/shell", 'w') file="data/shell" filesql="data/sql" #date_list.append() print("test") #for date_item in date_list: # print(date_item.replace('\n','')) tbname_list=whh("data/replyjob",file,filesql) #contactexp(tbname_list,date_list,cpexp,file)
感谢各位的阅读,以上就是“python怎么读文件”的内容了,经过本文的学习后,相信大家对python怎么读文件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。