這篇文章主要講解了“python怎么讀文件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“python怎么讀文件”吧!
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比張店網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式張店網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋張店地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
#!usr/bin/env python # encoding:utf-8 ###刪除包含關(guān)鍵字“ 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怎么讀文件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)python怎么讀文件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!