有一個代碼:拆分內部的Python
def readData():
file = open('data.txt', 'r')
listing = []
for line in file:
print(line.split())
depart = line.split()
m = [line.strip()]
listing.append(m)
file.close()
return listing
def display():
depart = readData()
poorest = []
if int(depart[1]) <= int(depart[2])+int(depart[3]):
poorest.append(depart[0])
print(poorest)
的 '離開' 輸出會產生這樣的:
[['Ladies 250 184 196'], ['Gentlemen 167 321 459'], ['Toys \t180 150 210'], ['Beauty\t450 280 320'], ['Technology\t169 320 279'], ['Home\t120 58 45'], ['Appliances\t210 130 67'], ['Food\t180 45 89'], ['Shoes\t260 100 210'], ['Children 179 50 80']]
但我需要產生這樣的:
['Ladies', '250', '184, '196']
每一個。我應該如何改變第二個功能?
試試這個,而不是'poorest.append(depart [0] .split())'在這裏你分裂給定的字符串在空間 – The6thSense