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

stdin.readline()會讀取行尾的換行符,使用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() 方法用于移除字符串頭尾指定的字符(默認為空格或換行符)或字符序列。
注意:該方法只能刪除開頭或是結尾的字符,不能刪除中間部分的字符。
推薦學習:《Python教程》
分享標題:創(chuàng)新互聯(lián)Python教程:python讀入不定行字符的問題
地址分享:http://fisionsoft.com.cn/article/dpgpjpj.html


咨詢
建站咨詢
