目錄
話不多說,直接重點!
一、數據預覽
可以看到,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
四、結果