简体   繁体   English

如何解决此rstrip属性错误?

[英]How do I fix this rstrip attribute error?

I'm using .rstrip to remove \\n from the words in the imported text file but its coming up as an attribute error? 我正在使用.rstrip从导入的文本文件中的单词中删除\\ n,但是它作为属性错误出现了?

AttributeError: 'list' object has no attribute 'rstrip' AttributeError:“列表”对象没有属性“ 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])

您在这里订购有误,您想要做的是:

lines[0].rstrip('\n')

lines is an array, rstrip is a method of string 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM