各位前輩大家好,因為剛踏入PYTHON不久,有些資料合併的問題需要請教各位前輩
這是txt的資料型態,我希望能轉換成excel檔案,刪除每欄的空白值並呈現這樣的形態
我嘗試了許多語法,可是都沒辦法
import xlwt
txtname='C:/Users/桌面/python語法資料/水泥工業/result.txt'
excelname='C:/Users/桌面/python語法資料/水泥工業/result.xlsx'
fopen=open(txtname,'r',encoding="utf-8")
lines=fopen.readlines()
file=xlwt.Workbook(encoding="utf-8",style_compression=0)
sheet=file.add_sheet('data')
i=0
for line in lines:
line=line.strip('\n')
line=line.split(' ')
id=line[0]
name=line[1]
year=line[2]
x2=line[3]
y2=line[4]
event=line[5]
sheet.write(i,0,id)
sheet.write(i,1,name)
sheet.write(i,2,year)
sheet.write(i,3,x2)
sheet.write(i,4,y2)
sheet.write(i,5,event)
i=i+1
file.save(excelname)
有問有大神可以幫幫我嗎?拜託大家
資料處理請找pandas
把txt讀進來用read_csv 參數給空白行
import pandas as pd
data = '123.csv'
column_names = ['欄位一','a','b','c','d','e','f','g','h','i','j']
data = pd.read_csv(data,comment='\t',names=column_names,sep='\s',engine='python')
print(data)
確認無誤存檔
df =pd.DataFrame(data)
df.to_excel('result.xlsx')
I appreciate you sharing this blog. For individuals who are looking for pertinent information, this is a huge aid. mapquest directions