代碼如下所示:import stddrawimport sysimport stdio#open the lexicon filelexicon_input_file = open(sys.argv[1])# copy the lexicon over from filelexicon = lexicon_input_file.readlines()#close the lexicon filelexicon_input_file.close()for i in range(len(lexicon)): #4000 words # breaks down entire lexicon into each element incremented from 0,1,2,3,4,.. line_of_text = lexicon[i] line_without_newline = line_of_text.rstrip('\n')print(lexicon)終端結(jié)果如下所示:['the\n', 'be\n', 'and\n', 'of\n', 'a\n', 'in\n', 'to\n', 'have\n', 'it\n'.......我如何從元素的末端擺脫 \n。我試過 rsplit 但不知道為什么它不起作用。我對編碼真的很陌生!
2 回答

慕碼人8056858
TA貢獻1803條經(jīng)驗 獲得超6個贊
這應(yīng)該刪除 '\n':
lexicon = [i.strip('\n') for i in lexicon] print(lexicon)
添加回答
舉報
0/150
提交
取消