我的代码从文件中获取信息并打印出相同的设计,但字符不同。例如,A.txt有一个文本文件,图像“A”由*和表示:但我需要它用大写和小写'a'打印
import os
def main():
# relative path to the folder where you unzipped the ASCII art files
#folder = "stars"
# or use an absolute path to the folder. example:
folder = r"C:\Users\sarah\Documents\a CPS 111\Bonus PA\stars\stars"
# os.listdir(folder) returns a list of files in folder
file_list = os.listdir(folder)
letter_art = {}
word = str(input("Please input a letter: "))
word = word.upper()
for fname in file_list:
letter_extension_list = fname.split(".")
for letter in word:
key = letter
value = letter_extension_list[1]
value = "%s."%(key) + value
letter_art[key] = value
fname = "\\".join([folder, value])
print(fname)
infile = open(fname)
for line in infile.readlines:
line = line.split()
if "*" in line:
print(word.upper())
else:
print(word.lower())
infile.close()
main()
现在我的错误是
TypeError: 'builtin_function_or_method' object is not iterable
答案 0 :(得分:0)
尝试将infile = open(fname).read()
更改为infile = open(fname)
你不能readlines()
来自字符串。
TypeError: 'builtin_function_or_method' object is not iterable
:您应该在内置方法()
之后添加readlines
,基本上您正在尝试迭代此方法的返回而不是方法本身。
如果您要检查字符串中是否有line = line.split()
,请同时删除*
,line.split()
会将line
变为[line]
答案 1 :(得分:0)
for line in infile.readlines:
readlines是一个函数,不能迭代。调用者可以调用函数并返回值。所以你只需要调用该函数并迭代它的返回值如下:
for line in infile.readlines():
pass