python清除欄位前後的的多餘空白
import csv #輸入csv套件comma separated value
file = open('20230525.csv','r')#打開下載的檔案,模式是r讀取,
reader = csv.reader(file) #將檔案逐列讀入串列變數csvreader
header, rows = [], [] #宣告空白串列(陣列,清單)
header = next(reader) #串列header儲存檔案第一列
for row in reader: #檔案接續逐列附加append於rows串列
rows.append(row) #檔案讀到header標題,以及rows串列
file.close() #關閉檔案
rowss=[] #建立一個空的串列
for row in rows: #檔案接續逐列附加append於rows串列
xyz = [] #臨時的列
for col in row:
xyz.append(col.strip())#逐欄位除掉右邊空白
rowss.append(xyz) #檔案讀到header標題,以及rows串列
f=open('myfile.csv','w',newline='',encoding='utf-8')
w = csv.writer(f) #寫入檔案
w.writerow(header) #寫入標題
w.writerows(rowss) #第7列將檔案內容名稱rows
f.close() #關閉檔案
https://ruubyyy0.blogspot.com/2024/05/excelpython.html
回覆刪除https://11217133.blogspot.com/2024/05/excel-python.html
回覆刪除https://meimei20041026.blogspot.com/2024/05/excelpython.html
回覆刪除https://hsiao-yu0711.blogspot.com/2024/05/excelpython.html
回覆刪除https://pcai766.blogspot.com/2024/05/excelpython.html
回覆刪除https://draft.blogger.com/blog/post/edit/1538851669311843874/5039915674634234397
回覆刪除https://sushshshaq119.blogspot.com/2024/05/excelpython.html
回覆刪除https://d11014153.blogspot.com/2024/05/excelpython.html
回覆刪除作者已經移除這則留言。
回覆刪除作者已經移除這則留言。
回覆刪除https://draft.blogger.com/blog/post/edit/7002249966937647944/2845470780777040800
回覆刪除https://the-dumbest-one.blogspot.com/2024/05/excelpython.html
回覆刪除https://ccp19890604.blogspot.com/2024/05/excelpython.html
回覆刪除https://huilii.blogspot.com/2024/05/excelpython.html
回覆刪除https://s10655075.blogspot.com/2024/05/excelpython.html
回覆刪除https://the-most-great-tony11.blogspot.com/2024/06/excelpython-5-30-2024.html
回覆刪除