[英]How can I make this alphabetical list look nicer in the output in python
它在大多數情況下都有效,但是輸出看起來像地獄般凌亂,所以我應該如何清理它,我還想知道如果可能的話,如何對偽代碼進行措辭。
#open file with list
infile = open("unsorted_fruits.txt", "r")
#open file writing to
outfile = open("sorted_fruits.txt", "w")
#create variable to work with list
all_lines = infile.readlines()
for line in all_lines:
print (line,)
#this function has sorted other list
def insertion_sort(list):
for index in range(1, len(list)):
value = list[index]
i = index - 1
while i >= 0:
if value < list[i]:
list[i+1] = list[i]
list[i] = value
i = i - 1
else:
break
#calling the function to sort
insertion_sort(all_lines)
all_sorted = str(all_lines)
#print list to show its sorted
print (all_sorted)
#write the sorted list to the file
outfile.write(all_sorted)
infile.close()
outfile.close()
exit()
輸入:木瓜
奇異果
扎波特·布蘭科
哈克貝利
香蕉
圖
酸橙
西瓜
香草
伊耶薩斯
羅望子
烏姆科洛
桲
蘋果
因布
接骨木漿果
六月
芒果
草莓
油桃
日期
櫻桃
橙子
西瓜
葡萄
覆盆子
輸出:['\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','apple \\ n','banana \\ n','cherry \\ n','date \\ n ','接骨木果\\ n','無花果\\ n','葡萄\\ n','哈克貝利\\ n','imbu \\ n','juneberry \\ n','奇異果\\ n','石灰\\ n', '芒果\\ n','油桃\\ n','橙色\\ n','木瓜\\ n','木瓜\\ n','樹莓\\ n','草莓\\ n','羅望子\\ n','umkolo \\ n','香草\\ n','西瓜\\ n','西瓜\\ n','yiessas \\ n','zapote blanco \\ n']
這可能是由於文件末尾有額外的換行符,這些額外的換行符已在readlines()
函數中獲取。
當您要打印最終列表中的每個項目時,只需過濾掉那些僅換行的項目即可:
for word in all_sorted:
if word.rstrip('\n'):
print(word.rstrip('\n'))
rstrip()
函數(右移)在單詞末尾rstrip()
所有換行符。 如果單詞只是換行符,則rstrip將返回一個空字符串,該字符串由if語句過濾掉。
問題出在這里,實際上不是輸出。 這是您閱讀水果清單的方式。 您真的想對水果進行排序,而不是對行進行排序。
輸入文件中的許多行都是空白的,您不想包含它們。 而且,您可能也不想將尾部的'\\ n'視為每個水果的一部分。
因此,我建議不要遍歷infile.readlines(),而建議遍歷infile.read()。splitlines()。 這將自動刪除尾隨\\ n。 當您使用它時,可以刪除空白行。
with open('unsorted_fruits.txt', 'r') as infile:
fruits = [f for f in infile.read().splitlines() if f != '']
然后,您可以對水果而非行進行排序。 如果您不希望將最終輸出寫為Python列表的字符串表示形式,則可以使用print()。
for fruit in fruits:
print(fruit, file=outfile)
或file.writelines()
outfile.writelines(f + '\n' for f in fruits)
現在,使用此代碼,輸出文件如下所示。 任何人都建議使它看起來更好。 applebananacherrydateelderberryfiggrapehuckleberryimbujuneberrykiwifruitlimemangonectarineorangepapayaquinceraspberrystrawberrytamarindumkolovanillawatermelonxiguayiessaszapote blanco ['\\ n','\\ n','\\ n','\\ n','\\ n','\\ n',' n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','\\ n','apple \\ n','banana \\ n','cherry \\ n','date \\ n ','接骨木果\\ n','無花果\\ n','葡萄\\ n','哈克貝利\\ n','imbu \\ n','juneberry \\ n','奇異果\\ n','石灰\\ n', '芒果\\ n','油桃\\ n','橙色\\ n','木瓜\\ n','木瓜\\ n','樹莓\\ n','草莓\\ n','羅望子\\ n','umkolo \\ n','香草\\ n','西瓜\\ n','西瓜\\ n','yiessas \\ n','zapote blanco \\ n']
#open file with list
infile = open("unsorted_fruits.txt", "r")
#open file writing to
outfile = open("sorted_fruits.txt", "w")
#create variable to work with list
all_lines = infile.readlines()
for line in all_lines:
print (line,)
#this function has sorted other list
def insertion_sort(list):
for index in range(1, len(list)):
value = list[index]
i = index - 1
while i >= 0:
if value < list[i]:
list[i+1] = list[i]
list[i] = value
i = i - 1
else:
break
#calling the function to sort and make it look nicer
insertion_sort(all_lines)
for word in all_lines:
if word.rstrip('\n'):
print(word.rstrip('\n'))
for word in all_lines:
if word.rstrip('\n'):
outfile.write(word.rstrip('\n'))
all_sorted = str(all_lines)
#write the sorted list to the file
outfile.write(all_sorted)
infile.close()
outfile.close()
exit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.