目录
话不多说,直接重点!
一、数据预览
可以看到,1、3列数据的格式有复数形式的,0、2、4列实数形式的并且为0,本次的目标是将每一个复数拆分成实部和虚部保存到两个txt文件中,用‘,’隔开,每行保存8个数据。
二、方法分析
- 建立实部.txt和虚部.txt两个文件
- 按行遍历处理.xls数据
- 判断当前列是否为0、2、4列,如果是,然后将0保存到“实部.txt”文件中,将0保存到“虚部.txt”文件中
- 若当前列为1、3列,则通过划分实部与虚部,并分别保存到“实部.txt”文件和“虚部.txt”文件
(后面将每个数乘以3,后并且将“-”号变为8,-3最终变成了89,)
三、代码示例
import xlrd
path = "C:\\Users\\LZN_GWI\\Desktop\\IFFT.xls"
path1 = "C:\\Users\\LZN_GWI\\Desktop\\"
file1 = open(path1 + "实部" + '.txt','a')
file2 = open(path1 + "虚部" + '.txt','a')
exc_data = xlrd.open_workbook(path)
table = exc_data.sheets()[0]
rows = table.nrows
cols = table.ncols
print(rows,cols)
count = 0
for i in range(cols):
if i == 0 or i == 2 or i == 4:
exc_cols_data = table.col_values(i)
for j in exc_cols_data:
if j != '':
# file1.write(str(int(j)) + ',')
# file2.write(str(int(j)) + ',')
if count != 8:
file1.write(str(int(j))+',')
file2.write(str(int(j))+',')
count += 1
else:
file1.write('\n')
file1.write(str(int(j)) + ',')
file2.write('\n')
file2.write(str(int(j)) + ',')
count = 1
else:
break
if i == 1 or i == 3:
exc_cols_data = table.col_values(i)
for j in exc_cols_data:
if j != '':
j = str(j)
j_new = j.split(' ')
if count != 8:
if j_new[0][0] == '-':
file1.write('8' + str(int(j_new[0][1]) * 3) + ',')
else:
file1.write(str(int(j_new[0][0]) * 3) + ',')
if j_new[1] == '-':
file2.write( '8' + str(int(j_new[2][0]) * 3) + ',')
elif j_new[1] == '+':
file2.write(str(int(int(j_new[2][0])) * 3) + ',')
count += 1
else:
file1.write('\n')
file2.write('\n')
if j_new[0][0] == '-':
file1.write('8' + str(int(j_new[0][1]) * 3) + ',')
else:
file1.write(str(int(j_new[0][0]) * 3) + ',')
if j_new[1] == '-':
file2.write( '8' + str(int(j_new[2][0]) * 3) + ',')
elif j_new[1] == '+':
file2.write(str(int(j_new[2][0]) * 3) + ',')
count = 1
else:
break
四、结果