names.txt文件中的数据输出显示为('ABC\n', 0)
,如何将其打印为ABC
name.txt
是德语名称
from PIL import Image, ImageDraw, ImageFont
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
filepath = 'names.txt'
fonttype = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf'
fntYU = ImageFont.truetype(fonttype, 150)
i=0
with open(filepath) as myfile:
for data in zip(myfile, range(100)):
i+=1
print (data)
if i == 100:
os.system("echo complete the 10 photots > complete.txt")
if (len(data)) <= 6:
img = Image.new('RGB', (3840, 2160), color = (255, 255, 255))
fnt = ImageFont.truetype(fonttype, 550)
d = ImageDraw.Draw(img)
d.text((600,700), data, font=fnt, fill=(0, 0, 0))
img.save(data.replace('\n', '')+".png")
错误:
('ABC\n', 0)
Traceback (most recent call last):
File "imagesv2.py", line 24, in <module>
d.text((600,700), data, font=fnt, fill=(0, 0, 0))
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageDraw.py", line 220, in text
mask, offset = font.getmask2(text, self.fontmode, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFont.py", line 181, in getmask2
size, offset = self.font.getsize(text, direction, features)
TypeError: expected string
答案 0 :(得分:1)
使用zip
有什么需要?
这应该有效-
import os
filepath = 'names.txt'
with open(filepath, 'r') as myfile:
for line in myfile:
print(line.rstrip('\n'))
编辑:如果要在100行之后中断,请使用enumerate
。例如-。
for index, line in enumerate(f):
print(line)
if index >= 100:
break
答案 1 :(得分:0)
import os
import sys
filepath = 'names.txt'
i=0
with open(filepath) as myfile:
for data in myfile:
i+=1
print (data.strip())