一键对数据进行反向计分
每次分析数据都觉得反向计分超级麻烦
无论是Excel操作还是spss都让人很烦,就写了一个python程序,从此再也不为此烦心啦
import numpy as np
import openpyxl
# 输入行数
row_num = int(input('行数'))
# 输入列数
vol_num = int(input('列数'))
# 获取反向计分后的TXT文件
def get_txt():
# 读取TXT源文件,数据以逗号分隔
b = np.genfromtxt(r'C:\Users\lenovo\Desktop\反向计分.txt', delimiter=',')
# 4点计分反向计分
# for i in range(2):
# for j in range(5):
# if b[i][j] == 1:
# b[i][j] = 4
# elif b[i][j] == 2:
# b[i][j] = 3
# elif b[i][j] == 3:
# b[i][j] = 2
# elif b[i][j] == 4:
# b[i][j] = 1
# print(b)
# 5点计分反向计分
# 通过循环进行反向计分
for i in range(row_num):
for j in range(vol_num):
if b[i][j] == 1:
b[i][j] = 5
elif b[i][j] == 2:
b[i][j] = 4
elif b[i][j] == 3:
b[i][j] = 3
elif b[i][j] == 4:
b[i][j] = 2
elif b[i][j] == 5:
b[i][j] = 1
print(b)
# 保存反向计分后的数据
np.savetxt(r'C:\Users\lenovo\Desktop\成功反向计分.txt', b, delimiter=',', fmt='%.2f')
# 将反向计分后的TXT文件转换成xlsx文件
def get_xlsx():
# 打开反向计分后获得的TXT文件
f = open(r'C:\Users\lenovo\Desktop\成功反向计分.txt', 'r', encoding='utf-8')
lines = f.readlines()
# 创建xlsx文件,指定活动表
wk = openpyxl.Workbook()
sheet = wk.active
# 活动表命名为'反向计分'
sheet.title = '反向计分'
# 从第0行开始
x = 0
# 遍历每一行并写入
for line in lines:
line = line.split(',')
for index in range(vol_num):
sheet.cell(x+1, index+1, line[index])
x += 1
# 保存
wk.save(r'C:\Users\lenovo\Desktop\成功反向计分.xlsx')
if __name__ == '__main__':
get_txt()
get_xlsx()
我贴心的把五点计分和四点计分的都写好啦
有需要的可以到后台找我要exe可执行文件
不早啦,晚安
参考链接:
https://blog.csdn.net/nanhuaibeian/article/details/108949626?utm_term=python%E5%B0%86txt%E4%BF%9D%E5%AD%98%E4%B8%BAexcel%E6%96%87%E4%BB%B6&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-1-108949626&spm=3001.4430
赞 (0)