Python 练习 第 0014 题 第 0015 题 第 0016 题 读取 txt文件 写入成 xls文件
2018-04-03 17:34
447 查看
student.txt
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}city.txt
{
"1":"上海",
"2":"北京",
"3":"成都"
}numbers.txt
[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]直接上代码
import json
import xlwt
from collections import OrderedDict
wb = xlwt.Workbook() # 创建一个工作簿
# 创建一个Sheet
def getSheet(strv):
return wb.add_sheet(strv)
# 读取txt文件里面的内容
def ReadTxt(txt):
with open(txt+'.txt', encoding='utf-8') as f:
vvv = json.load(f)
my_dict = {}
if type(vvv) == list:
for i in range(0, len(vvv)):
my_dict[i] = vvv[i]
else:
my_dict = OrderedDict(vvv)
return my_dict
def writeExcel(listTitle, sheetName):
ws = getSheet(sheetName)
# 标题
for i in range(0, len(listTitle)):
ws.write(0, i, listTitle[i])
# 内容
row = 1
content_dict = ReadTxt(sheetName)
for k, v in content_dict.items():
# 序号
ws.write(row, 0, k)
col = 1
if type(v) != list and v.isalnum():
ws.write(row, col, v)
else:
for item in v:
ws.write(row, col, item)
col += 1
row += 1
wb.save(sheetName+'1.xls')
if __name__ == '__main__':
# 标题
# 0014题
# listStr = ['序号', '姓名', '英语', '数学', '语文']
# writeExcel(listStr, 'student')
# 0015题
# listStr = ['序号', '城市名']
# writeExcel(listStr, 'city')
# 0016题
listStr = ['序号', '数字1', '数字2', '数字3']
writeExcel(listStr, 'numbers')
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tavatimsa/article/details/79806599
{
"1":["张三",150,120,100],
"2":["李四",90,99,95],
"3":["王五",60,66,68]
}city.txt
{
"1":"上海",
"2":"北京",
"3":"成都"
}numbers.txt
[
[1, 82, 65535],
[20, 90, 13],
[26, 809, 1024]
]直接上代码
import json
import xlwt
from collections import OrderedDict
wb = xlwt.Workbook() # 创建一个工作簿
# 创建一个Sheet
def getSheet(strv):
return wb.add_sheet(strv)
# 读取txt文件里面的内容
def ReadTxt(txt):
with open(txt+'.txt', encoding='utf-8') as f:
vvv = json.load(f)
my_dict = {}
if type(vvv) == list:
for i in range(0, len(vvv)):
my_dict[i] = vvv[i]
else:
my_dict = OrderedDict(vvv)
return my_dict
def writeExcel(listTitle, sheetName):
ws = getSheet(sheetName)
# 标题
for i in range(0, len(listTitle)):
ws.write(0, i, listTitle[i])
# 内容
row = 1
content_dict = ReadTxt(sheetName)
for k, v in content_dict.items():
# 序号
ws.write(row, 0, k)
col = 1
if type(v) != list and v.isalnum():
ws.write(row, col, v)
else:
for item in v:
ws.write(row, col, item)
col += 1
row += 1
wb.save(sheetName+'1.xls')
if __name__ == '__main__':
# 标题
# 0014题
# listStr = ['序号', '姓名', '英语', '数学', '语文']
# writeExcel(listStr, 'student')
# 0015题
# listStr = ['序号', '城市名']
# writeExcel(listStr, 'city')
# 0016题
listStr = ['序号', '数字1', '数字2', '数字3']
writeExcel(listStr, 'numbers')
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tavatimsa/article/details/79806599
相关文章推荐
- Python 读取EXCEL(XLS、CSV)写入txt文件
- Python每日练习 14 15 16 将某txt的内容写入到xls文件中
- python读取 .txt 文本内容以及将程序执行结果写入txt文件
- Python读取指定路径所有.txt文件,将文件名写入.txt
- python读取一个目录下的文件,并且写入.txt文件中
- python读取.txt文件数据和将数据写入文件.txt
- C# Python 读取txt中的文件,将16进制的数据按照小端方式排列,处理为有符号的数字,并写入text文件;控制台和界面两种方式
- python 将百度搜索风云榜的关键内容提取并写入txt文件和读取验证
- P-1.7 [有例题]Python读取、写入.txt文件
- Python读取txt内容写入xls格式excel中的方法
- python3将csv文件中的两列数据读取出来,并且按行写入txt文本之中
- 用python读取、写入txt文件
- Python将读取的SQL查询记录写入xls文件中
- Python txt文件读取写入字典的方法(json、eval)
- python读取TXT文件并逐行写入另外一个TXT文件
- 第十七章,txt文件的写入和读取数据结合练习(C++)
- python 写入、读取txt文件
- python菜鸟笔记--python读取和写入txt文件的方法
- Python读取txt内容写入xls格式的excel中
- [文件] Python读取txt存储至xls