.py目錄下有
1.xlsx,2.xlsx,3.xlsx
有一個1.txt,內容是
1
2
3
python讀取1.txt根據內容轉換對應xlsx為csv
我現在txt讀取出來了,存入數組,再讀取數組轉換xlsx只能轉換最後一個xlsx,打印的時候是1,2,3,數組只有最後一個,不明白為什麼
import numpy as np
import pandas as pd
i=0
with open('123.txt','r') as f:
for line in f.readlines():
line = line.strip().split()
i+=1
b =line[0:1]
lines = np.array(b)
print(lines)
for i in range(0,len(lines)):
print(lines[i])
data = pd.read_excel(lines+'.xlsx','Sheet1',index_col=0)
data.to_csv('data.csv',encoding='utf-8')