我使用.rstrip从导入的文本文件中的单词中删除\ n但是它出现属性错误?
AttributeError:' list'对象没有属性' rstrip'
import random
import os
import time
text_file = open ("ten_words.txt", "r")
lines = text_file.readlines()
missing = random.choice(lines)
random.shuffle(lines)
print ("First Grid")
print ("\n", lines.rstrip[0],"|", lines.rstrip[1], "|", lines.rstrip[2])
print ("\n", lines.rstrip[3],"|", lines.rstrip[4], "|", lines.rstrip[5])
print ("\n", lines.rstrip[6],"|", lines.rstrip[7], "|", lines.rstrip[8])
答案 0 :(得分:1)
你这里有一些错误,你想做的是:
lines[0].rstrip('\n')
答案 1 :(得分:0)
lines是一个数组,rstrip
是一个字符串
import random
import os
import time
text_file = open ("ten_words.txt", "r")
lines = text_file.readlines()
missing = random.choice(lines)
random.shuffle(lines)
print ("First Grid")
print ("\n", lines[0].rstrip(),"|", lines[1].rstrip(), "|", lines[2].rstrip())
print ("\n", lines[3].rstrip(),"|", lines[4].rstrip(), "|", lines[5].rstrip())
print ("\n", lines[6].rstrip(),"|", lines[7].rstrip(), "|", lines[8].rstrip())