Skip to content

Commit

Permalink
拼装Excel表格单行数据为字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcuizy committed Mar 1, 2021
1 parent a634efe commit 2bd6d70
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions 拼接表格单行数据为字符串/join_excel_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
拼接Excel表格单行数据,并写入文本
author: gxcuizy
time: 2021-03-01
"""

import pandas
import random
import os
import time


def echo_msg(msg=''):
"""打印信息"""
now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print('[' + now_time + '] ' + msg)


# 程序主入口
if __name__ == "__main__":
# 获取传入参数
file_name = input('请输入当前目录下的表格文件名(例如“01.xlsx”):')
line_num = input('请输入要拼装的数据第几列(例如“1”):')
# 判断文件是否存在
if os.path.exists(file_name) == False:
echo_msg('文件不存在')
os.system("pause")
exit(0)
# 判断输入的行数是否为数字
if line_num.isdigit() == False:
echo_msg('请输入列数的数字')
os.system("pause")
exit(0)
try:
# 获取表格数据
echo_msg('开始获取文件[' + file_name + ']的第[' + str(line_num) + ']列数据')
line_num = int(line_num) - 1
sheet = pandas.read_excel(io=file_name, usecols=[line_num])
data = sheet.values.tolist()
str_data = ''
# 循环处理数据
echo_msg('已获取列数据条数[' + str(len(data)) + '],开始处理数据……')
for x in range(len(data)):
if str(data[x][0]) != 'nan':
str_data += "'" + str(data[x][0]) + "',"
# 写入文本文件
echo_msg('数据处理完毕,开始写入……')
random_num = random.randint(1000, 9999)
with open('str_' + str(random_num) + '.txt', 'w') as f:
f.write(str_data.strip(','))
echo_msg('数据写入完毕.')
except Exception as err_info:
# 异常信息
echo_msg(str(err_info))
# 防止exe程序执行结束闪退
os.system("pause")
Binary file not shown.

0 comments on commit 2bd6d70

Please sign in to comment.