输入为:
要求输出为:
代码:
file = open('test.txt','rb') file_context = file.read().decode('utf-8') #print ("整个文件内容:", file_context) #写文件 outdata = open('test1.txt', 'w', encoding="utf-8") # 以换行符为分隔符,从而将这个文件以行为单位进行分割 line = file_context.split('\n') for i in range(0, len(line)): #读取每一行 line_context = line[i] # print ("line_context:", line_context) 宝马 奔驰:0.9,奥迪:0.8,hh:99 # 先以 ,为分割符进行分割 line_context_split = line_context.split(',') #print(line_context_split)['\ufeff宝马 奔驰:0.9', '奥迪:0.8', 'hh:99\r'] #将宝马取出来 first_split = line_context_split[0].split(" ") first = first_split[0] # print ("first:", first) 宝马 for j in range(0, len(line_context_split)): # print (j, line_context_split[j]) #['\ufeff宝马 奔驰:0.9', '奥迪:0.8', 'hh:99\r'] if j == 0: if "" in line_context_split[j] and ":" in line_context_split[j]: string = line_context_split[j].replace(" ", "\t").replace(":","\t") outdata.write(string+'\n') elif j >= 1: if ":" in line_context_split[j]: line_context_split[j] = line_context_split[j].replace(":","\t") string = first + "\t"+ line_context_split[j] outdata.write(string+'\n')
文件读取的时候是全文读取,效率低下,使用按行读取会出现空行,用另一段代码将空行删除。
file = open('test.txt',encoding='utf-8') #print ("整个文件内容:", file_context) #print ("--------------------------------") #写文件 outdata = open('test1.txt', 'w', encoding="utf-8") # 以换行符为分隔符,从而将这个文件以行为单位进行分割 for line in file.readlines(): #读取每一行 #line_context = str(line) #为分割符进行分割 line_context_split = line.split(',') #print(line_context_split)['\ufeff宝马 奔驰:0.9', '奥迪:0.8', 'hh:99\r'] #将宝马取出来 first_split = line_context_split[0].split(" ") first = first_split[0] # print ("first:", first) 宝马 for j in range(0, len(line_context_split)): # print (j, line_context_split[j]) #['\ufeff宝马 奔驰:0.9', '奥迪:0.8', 'hh:99\r'] if j == 0: if "" in line_context_split[j] and ":" in line_context_split[j]: string = line_context_split[j].replace(" ", "\t").replace(":","\t") outdata.write(string+'\n') elif j >= 1: if ":" in line_context_split[j]: line_context_split[j] = line_context_split[j].replace(":","\t") string = first + "\t"+ line_context_split[j] outdata.write(string+'\n')
删除空行代码:
# coding:utf-8 def delblankline(infile, outfile): infp = open(infile, "r",encoding='utf-8') outfp = open(outfile, "w",encoding='utf-8') lines = infp.readlines() for li in lines: if li.split(): outfp.writelines(li) infp.close() outfp.close() # 调用示例 if __name__ == "__main__": delblankline("test1.txt", "2.txt")