簡體   English   中英

從數組中刪除開頭的空格

[英]remove spaces at the beginning from array

我想在一開始就從數組中刪除空格。

這是我的代碼:

f = open("demofile.txt", "r")
lines = f.readlines()
for i in list(lines):
    w = i[3:]
    w = ', '.join(w.split())
    #print(w)
    #time.sleep(1)
    y = i[2]
    y=int(y)+1
    #print(y)
    c1=np.array([w])
    c1 = [int(i) for i in c1[0].replace(" ", "").split(",")]
    c1=np.array([c1]*3)
    c1=np.transpose(c1)
    a=str(c1).replace("[",'')
    a=str(a).replace("]",'')
    print(a)

輸入: <=1 2011 2021 2031我的 Output:

2011 2011 2011
 2021 2021 2021
 2031 2031 2031

我需要:

2011 2011 2011
2021 2021 2021
2031 2031 2031

我試過 function 條

嘗試在print(a)之前添加這一行: a=str(a).replace("\n ",'\n') \n表示換行,所以如果一行中的第一個字母是空格,它將被刪除。

更清潔的選項如下:

a = ""
for row in c1:
    a = f'{a}{" ".join(map(str, row))}\n'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM