文件处理python---txt转excel

# -*- 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字符串,这样就不会处理其中的转义了。否则,可能会报错

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值