文件处理python---txt转excel
2018-02-14 14:57
591 查看
# -*- coding: utf-8 -*- """ Created on Fri Feb 9 19:59:05 2018 @author: Administrator """ # -*- coding: utf-8 -*- #导入xlwt模块 import xlwt # 创建一个Workbook对象,这就相当于创建了一个Excel文件 book = xlwt.Workbook(encoding='utf-8', style_compression=0) ''' Workbook类初始化时有encoding和style_compression参数 encoding:设置字符编码,一般要这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。 默认是ascii。当然要记得在文件头部添加: #!/usr/bin/env python # -*- coding: utf-8 -*- style_compression:表示是否压缩,不常用。 ''' #创建一个sheet对象,一个sheet对象对应Excel文件中的一张表格。 # 在电脑桌面右键新建一个Excel文件,其中就包含sheet1,sheet2,sheet3三张表 sheet = book.add_sheet('test', cell_overwrite_ok=True) # 其中的test是这张表的名字,cell_overwrite_ok,表示是否可以覆盖单元格,其实是Worksheet实例化的一个参数,默认值是False ##########################################百度地图############################################## import requests import json lat = 49.2848290000 lng = -123.1221780000 pois = 0 def locatebyLatLng(lat, lng, pois=0): items = {'location': str(lat) + ',' + str(lng), 'ak': 'U3jTrAdpAx9BrQGx8xmo8cGo8UjhKERM', 'output': 'json'} res = requests.get('http://api.map.baidu.com/geocoder/v2/', params=items) result = res.json() #result = result['result']['formatted_address'] + ',' + result['result']['sematic_description'] result1 = result['result']['addressComponent']['country'] result2 = result['result']['addressComponent']['city'] result3 = result['result']['addressComponent']['street'] result = result1 + '-' + result2 + '-' + result3 return result #locatebyLatLng(lat, lng, pois=0) ################################### #读入txt内容 patch_file_name="C:/Users/Administrator/Desktop/Tesla_charger_sites_js.txt" patch_file=open(patch_file_name,'r') #打开文档,逐行读取数据 for line in open(patch_file_name): line=patch_file.readline() e5 = 0 countline = line.count('},',0)######计数 for i in range(countline+1): s1 = line.find('"location_id":"',e5+1) e1 = line.find('","location_type"',s1+1) w1 = line[s1+15:e1] e2 = line.find(',"open_soon":',e1+1) w2 = line[e1+20:e2-2] w2 = w2.replace('","',',') e3 = line.find('"latitude"',e2+1) w3 = line[e2+14:e3-2] e4 = line.find('"longitude"',e2+1) w4 = line[e3+12:e4-2] e5 = line.find('{"',e2+1) w5 = line[e4+13:e5-3] w6 = locatebyLatLng(w4, w5, pois=0) sheet.write(i, 0, w1) # 其中的'0-行, 0-列'指定表中的单元,'EnglishName'是向该单元写入的内容 sheet.write(i, 1, w2) sheet.write(i, 2, w3) sheet.write(i, 3, w4) sheet.write(i, 4, w5) sheet.write(i, 5, w6) book.save(r'e:\test1.xls') ''' #读取start_str到end的内容 def txt_wrap_by(start_str, end, html): start = html.find(start_str) if start >= 0: start += len(start_str) end = html.find(end, start) if end >= 0: return html[start:end].strip() start_str = ":" end = ',' print(txt_wrap_by(start_str ,end ,line)) ''' ''' ################################### # 向表test中添加数据 sheet.write(0, 0, 'EnglishName') # 其中的'0-行, 0-列'指定表中的单元,'EnglishName'是向该单元写入的内容 sheet.write(1, 0, 'Marcovaldo') txt1 = '中文名字' sheet.write(0, 1, txt1.decode('utf-8')) # 此处需要将中文字符串解码成unicode码,否则会报错 txt2 = '马可瓦多' sheet.write(1, 1, txt2.decode('utf-8')) ''' # 最后,将以上操作保存到指定的Excel文件中 #book.save(r'e:\test1.xls') # 在字符串前加r,声明为raw字符串,这样就不会处理其中的转义了。否则,可能会报错
相关文章推荐
- Python处理unicode编码的txt文件(Python中文处理)——解决to_excel()和to_csv()导出文件内容为空的问题
- Python学习笔记-Txt文件转Excel文件
- python处理txt文件
- 【Python文件处理】递归批处理文件夹子目录内所有txt数据
- Python对CSV、Excel、txt、dat文件的处理
- Python数据处理-将数据保存为txt、csv等文件格式方法
- Python--使用xlwt将列表类型的数据写到Excel xls文件中--TxtToExcel
- python 将txt文件转换为excel
- 1、Python django 框架下的word Excel TXT Image 等文件的上传
- 玩转excel===Excel处理txt文件中的数据,Excel中的分列处理
- Python处理Excel文件实例代码
- python-读取excel,txt,csv文件
- python读取excel中表结构生成sql语句,存入txt文件
- python操作excel文件并输出txt文件
- python导入处理txt文件
- python操作excel文件并输出txt文件的实例
- nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件
- C# Python 读取txt中的文件,将16进制的数据按照小端方式排列,处理为有符号的数字,并写入text文件;控制台和界面两种方式
- 使用python处理中文csv文件,并让excel正确显示中文(避免乱码)
- python处理txt文件