将python结果打印到txt文件
我正在尝试将程序的结果打印到用户指定的文件中。 我的程序创建文件,但不写入文件。我的代码按我的要求工作,只是打印有问题。我需要随机文件字符串打印到由界面用户命名的文件中将python结果打印到txt文件,python,printing,Python,Printing,我正在尝试将程序的结果打印到用户指定的文件中。 我的程序创建文件,但不写入文件。我的代码按我的要求工作,只是打印有问题。我需要随机文件字符串打印到由界面用户命名的文件中 from random import randrange from random import choice def randomShape(): x = randrange (0,400) y = randrange (0,400) x2 = randrange(0,400) y2 = ra
from random import randrange
from random import choice
def randomShape():
x = randrange (0,400)
y = randrange (0,400)
x2 = randrange(0,400)
y2 = randrange(0,400)
radius = randrange (0,100)
red = randrange (192, 208)
blue = randrange(100,140)
green = randrange(150,175)
shape = ['cirle;','rectangle;']
randomShape = choice (shape)
if randomShape == 'cirle;':
print(randomShape,x,",",y,";",radius,";",red,",",blue,",",green)
elif randomShape != 'cirle;':
print(randomShape,x,",",y,";",x2,",",y2,";",red,",",blue,",",green)
def getColor( colorString ):
tokens = colorString.split(',')
if len( tokens ) == 3:
return color_rgb( int( tokens[0] ), int(tokens[1]), int( tokens[2] ))
elif len( colorString ) > 0:
return colorString.strip()
else:
return 'white'
def main():
kFile = input("Enter the drawing file name to create:")
openfile = open(kFile,"w")
q = int(input("Enter the number of shapes to make:"))
for x in range(q):
print (randomShape(),kFile)
main()
您可以尝试以下方法:
from random import randrange
from random import choice
from graphics import color_rgb
def randomShape():
x = randrange (0,400)
y = randrange (0,400)
x2 = randrange(0,400)
y2 = randrange(0,400)
radius = randrange (0,100)
red = randrange (192, 208)
blue = randrange(100,140)
green = randrange(150,175)
shape = ['cirle;','rectangle;']
randomShape = choice(shape)
if randomShape == 'cirle;':
print(randomShape,x,",",y,";",radius,";",red,",",blue,",",green)
elif randomShape != 'cirle;':
print(randomShape,x,",",y,";",x2,",",y2,";",red,",",blue,",",green)
return randomShape
def getColor( colorString ):
tokens = colorString.split(',')
if len( tokens ) == 3:
return color_rgb( int( tokens[0] ), int(tokens[1]), int( tokens[2] ))
elif len( colorString ) > 0:
return colorString.strip()
else:
return 'white'
def main():
kFile = input("Enter the drawing file name to create:")
q = int(input("Enter the number of shapes to make:"))
with open(kFile, "w") as file:
for x in range(q):
file.write(randomShape())
main()
在上面的代码中,必须从randomShape()
返回randomShape
值,并将输出写入文件。还有一点需要注意的是,您应该考虑使用诸如<代码>打开(kfile,a)的AppEnter模式,以继续添加到现有的文件中,并且每次运行该函数时不覆盖它。
print (randomShape(),kFile)
这不会写入文件。
使用以下命令打开文件后:
openfile = open(kFile,"w")
现在在openFile中有了一个文件句柄。您可以使用上面的方法写出数据(并确保您也关闭了文件)。一般来说,您可以使用write方法-
所以在你的情况下,你应该使用
kFile.write(randomShape())
另请注意,randomShape()
函数也不会向函数调用返回任何数据。因此,您可能需要修改它,以便在以下位置使用return
而不是print
:
打印(randomShape,x,,,,,y,;,,radius。正如MarkMeyer所指出的,您还需要写入文件对象,而不是使用其名称字符串。
结合这两种信息,尝试以下内容:
filename=输入(“文件名:”)
打开(文件名为“w”)作为openedFile:
打印(“一些数据”,file=openedFile)
很难确定您在尝试什么,以及您会遇到什么错误。程序是否成功打印到屏幕上?此外,您应该在完成后添加文件关闭条件(例如,kFile.close()
)。我需要将randomShape()的结果打印到用户输入名称@“输入要创建的图形文件名:”时创建的文件中,程序不会接受该函数。我在主文件的第43行得到一个文件“/Users/user/Downloads/join.py”。write(randomShape())TypeError:write()参数必须是str,而不是noneplace File。write(randomShape())为File.write(str(randomShape()))查看此输出:输入要创建的图形文件名:abcde.txt输入要创建的形状数:3圈;249 , 61 ; 15 ; 200、138、168圈;360 , 333 ; 18 ; 202、128、152个矩形;397 , 130 ; 140 , 7 ; 207116 160好了,现在我明白了。由于没有从randomShape()返回任何内容,因此您没有得到任何结果。因此,file.open()方法中没有任何内容。尝试从randomShape()返回randomShape值,我正在执行的操作。这就是为什么我没有得到任何错误。查看我的代码。而且,如果您想多次附加到同一个文件,请不要忘记使用附加模式。你也是这样出来的吗?我希望每个字符串都在文件中自己的行中具有随机形状。抱歉,如果我听起来像个白痴,但我对编码非常陌生,它会打印出形状,但不会打印出每个形状的值(整数)。当我更改它以返回时,它会打印出所有逗号和不需要的东西。我需要这样打印:矩形;127, 143; 318, 332; 32, 208, 253