您好,登錄后才能下訂單哦!
這篇文章主要講解了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。