您的位置:首页 > 编程语言 > Python开发

文件处理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字符串,这样就不会处理其中的转义了。否则,可能会报错
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: