新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:python讀入不定行字符的問(wèn)題
python讀入不定行字符的問(wèn)題解決:

stdin.readline()會(huì)讀取行尾的換行符,使用strip()函數(shù)去掉:
示例
import sys
from builtins import str
ingredient_list = set()
while True:
line = sys.stdin.readline().strip()
if line == '':
break
food_list = str(line).split(' ')
for ingredient in food_list:
ingredient_list.add(ingredient)
print(len(ingredient_list))Python strip() 方法用于移除字符串頭尾指定的字符(默認(rèn)為空格或換行符)或字符序列。
注意:該方法只能刪除開(kāi)頭或是結(jié)尾的字符,不能刪除中間部分的字符。
推薦學(xué)習(xí):《Python教程》
名稱(chēng)欄目:創(chuàng)新互聯(lián)Python教程:python讀入不定行字符的問(wèn)題
分享網(wǎng)址:http://www.dlmjj.cn/article/dpgpjpj.html


咨詢(xún)
建站咨詢(xún)
