目錄
一、數據預覽
二、方法分析
- 建立實部.txt和虛部.txt兩個文件
- 按行遍歷處理.xls數據
- 判斷當前數據的類型是否是“float”,如果不是直接爲實數,將其存入到“實數.txt”文件中,將0存入到“虛數.txt”文件中(轉爲16進制)
- 如果是“float”類型,則爲複數,需要提取實部與虛部,並分別保存到“實部.txt”文件和“虛部.txt”文件(轉化爲16進制)
(需要將負數加上256)
三、代碼示例
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')
# file1 = open("C:\\Users\\LZN_GWI\\Desktop\\實部.txt",mode= 'a')
# file2 = open("C:\\Users\\LZN_GWI\\Desktop\\虛部.txt",mode= 'a')
exc_data = xlrd.open_workbook(path)
table = exc_data.sheets()[1]
rows = table.nrows
cols = table.ncols
print(rows,cols)
count = 0
for i in range(cols):
exc_cols_data = table.col_values(i)
for j in exc_cols_data:
#print(type(j))
if isinstance(j,float):
if j != '':
if count != 8:
if j<0:
file1.write(str(hex(-int(j) + 256))[2:]+',')
file2.write(str(hex(0))[2:] +',')
else:
file1.write(str(hex(int(j)))[2:] + ',')
file2.write(str(hex(0))[2:] + ',')
count += 1
else:
file1.write('\n')
file2.write('\n')
if j < 0:
file1.write(str(hex(-int(j) + 256))[2:] + ',')
file2.write(str(hex(0))[2:] + ',')
else:
file1.write(str(hex(int(j)))[2:] + ',')
file2.write(str(hex(0))[2:] + ',')
count = 1
else:
if count != 8:
if j[0] == '-':
for q in range(1, len(j)):
if j[q] == '-' or j[q] == '+':
file1.write(str(hex(int(j[1:q])+256))[2:] + ',')
else:
file1.write(str(hex(int(j[0])))[2:] + ',')
if j[-2] == '-' or j[-2] == '+':
if j[-2] == '-':
file2.write(str(hex(257))[2:] + ',')
else:
file2.write(str(hex(1))[2:] + ',')
else:
for p in range(1, len(j)):
if j[p] == '-' or j[p] == '+':
if j[p] == '-':
file2.write(str(hex(int(j[p + 1:-1]) + 256))[2:] + ',')
else:
file2.write(str(hex(int(j[p + 1:-1])))[2:] + ',')
count += 1
else:
file1.write('\n')
file2.write('\n')
if j[0] == '-':
file1.write(str(hex(int(j[1]) + 256))[2:] + ',')
else:
file1.write(str(hex(int(j[0])))[2:]+ ',')
if j[-2] == '-' or j[-2] == '+':
if j[-2] == '-':
file2.write(str(hex(257))[2:] + ',')
else:
file2.write(str(hex(1))[2:] + ',')
else:
for p in range(1, len(j)):
if j[p] == '-' or j[p] == '+':
if j[p] == '-':
file2.write(str(hex(int(j[p + 1:-1]) + 256))[2:] + ',')
else:
file2.write(str(hex(int(j[p + 1:-1])))[2:] + ',')
count = 1
四、結果