我有這個資料:
Event|Time|Meta|Meet|Date
50 Y Free|22.30|U|IL NASA Winter Blast Off|Nov 30, 2018
100 Y Free|55.50|U|Greg's Super Splash|Jun 16, 2011
50 Y Breast|27.07|X|CCIW Swimming Championships|Feb 10, 2006
50 L Breast|33.70||1st Cyprus International Masters Swim Meet|Oct 22, 2016
100 Y Breast|58.97||CCIW Swimming Championships|Feb 10, 2006
100 L Breast|1:14.24||42nd Bulgarian Masters Championship|Sep 07, 2020
200 Y Breast|2:06.45|U|Greg's Super Splash|Jun 16, 2011
200 L Breast|2:47.48||1st Cyprus International Masters Swim Meet|Oct 22, 2016
200 Y IM|2:03.71||CCIW Swimming Championships|Feb 09, 2006
200 S IM|2:39.60||3rd International Masters Tournament - Rodopa Smolyan|Apr 02, 2022
200 L IM|2:39.29||1st Cyprus International Masters Swim Meet|Oct 22, 2016
我想用“|”分隔資料 同時確保每列與它最長的“元素”對齊,如下所示:
Event Time Meta Meet Date
50 Y Free 22.30 U IL NASA Winter Blast Off Nov 30, 2018
100 Y Free 55.50 U Greg's Super Splash Jun 16, 2011
50 Y Breast 27.07 X CCIW Swimming Championships Feb 10, 2006
50 L Breast 33.70 1st Cyprus International Masters Swim Meet Oct 22, 2016
100 Y Breast 58.97 CCIW Swimming Championships Feb 10, 2006
100 L Breast 1:14.24 42nd Bulgarian Masters Championship Sep 07, 2020
200 Y Breast 2:06.45 U Greg's Super Splash Jun 16, 2011
200 L Breast 2:47.48 1st Cyprus International Masters Swim Meet Oct 22, 2016
200 Y IM 2:03.71 CCIW Swimming Championships Feb 09, 2006
200 S IM 2:39.60 3rd International Masters Tournament - Rodopa Smolyan Apr 02, 2022
200 L IM 2:39.29 1st Cyprus International Masters Swim Meet Oct 22, 2016
因此,我在 python 中創建了一個演算法,將資料拆分為行,然后將該資料拆分為列,找到每列的最長行,然后根據需要將每個條目以間距寫入輸出檔案:
def beautify_data(swimmer_name):
with open(f"assets\\data\\{swimmer_name}.txt", "r", encoding="UTF-8") as src:
data = src.read()
rows = data.split()
with open(f"assets\\data\\{swimmer_name}.txt", "w", encoding="UTF-8") as dst:
for row in rows:
entry = ""
max_length = 0
cols = row.split("|")
for col in cols:
max_length = col.__len__() if col.__len__() > max_length else max_length
for col in cols:
entry = col.strip()
for i in range(0, max_length):
entry = " "
dst.write(f"{entry.strip()}\n")
return f"{swimmer_name}.txt"
但由于某種原因,輸出不如預期。我明白了:
Event Time Meta Meet Date
50
Y
Free 22.30 U IL
NASA
Winter
Blast
Off Nov
30,
2018
100
Y
Free 55.50 U Greg's
Super
Splash Jun
16,
2011
50
Y
Breast 27.07 X CCIW
etc.
而不是我想要的。對我來說,似乎每一列都因某種原因被空格分割,這在代碼中并不明確。從概念上講,代碼是有意義的。有誰知道為什么我沒有得到想要的輸出?謝謝!
uj5u.com熱心網友回復:
rows = data.split()將整個檔案拆分為單詞串列。str.split()在空格上沒有引數拆分。您可能想要data = src.readlines()在上面的行中,它將檔案逐行讀取到串列中。
def beautify_data(swimmer_name):
with open(f"assets\\data\\{swimmer_name}.txt", "r", encoding="UTF-8") as src:
rows = src.readlines()
with open(f"assets\\data\\{swimmer_name}.txt", "w", encoding="UTF-8") as dst:
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520256.html
