Bu yüzden bir satırın neden dosya oluşturduğunu ve herhangi bir hatayı öntanımlı olarak yazdıracağım bir program var. Ben geçersiz satırları bekliyor geçersiz giriş hatlarında koyarsanızPython - Öznitelik hatası, 'NoneType' nesnesinin özniteliği yok
for line in lines_file:
#get offset up to start of coordinates
start = re.compile('\s*line\s*')
m = start.match(line)
offset = m.end()
try:
for i in range(4):
xy = re.compile('\s*([-]?[0-9]{1,3})\s*')
if xy.match(line,offset):
m = xy.match(line,offset)
else:
raise Exception
coordinate = m.group(1)
if int(coordinate) > 250 or int(coordinate) < -250:
raise Exception
offset = m.end()
end = re.compile('\s*$')
if not end.match(line,offset):
raise Exception
except Exception as e:
print >> sys.stderr, 'Error in line ' + str(line_number) + ":"
print >> sys.stderr, " " * 4 + line,
print >> sys.stderr, " " * (offset + 4) + "^"
line_number = line_number + 1
continue
Ve stderr'e basılacak, ben olsun çıkışı:
Traceback (most recent call last):
File "lines_to_svg.py", line 37, in <module>
offset = m.end()
AttributeError: 'NoneType' object has no attribute 'end'
bu benim kod parçasıdır olduğundan, hat 37 olduğu offset = m.end()
diyor. Öyleyse neden bir özellik hatası alıyorum? İşte her ihtimale karşı bu hataya neden olan yukarıdaki döngü önce gelir kod:
import sys
import re
# SVG header with placeholders for canvas width and height
SVG_HEADER = "<svg xmlns=\"http:#www.w3.org/2000/svg\" version=\"1.1\""" width=\"%d\" height=\"%d\">\n"
# SVG bounding box with placeholders for width and height
SVG_BOUNDING_BOX = "<rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\""" style=\"stroke:#000;fill:none\" />\n"
# SVG line with placeholders for x0, y0, x1, y1
SVG_LINE = "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\""" style=\"stroke:#000\" />\n"
# SVG footer
SVG_FOOTER = "</svg>"
CANVAS_HEIGHT = 500
CANVAS_WIDTH = 500
# process command line arguments
if len(sys.argv) != 2:
print >> sys.stderr, "Usage:", str(sys.argv[0]), "lines_file"
exit(1)
#open file for reading
try:
lines_file = open(sys.argv[1], 'r')
except IOError:
print >> sys.stderr, "Cannot open:", str(sys.argv[1])
exit(2)
offset = 0
line_number = 1
sorun offset = m.end()
ile ama o hataya neden olduğunu anlamaya gibi olamaz.
Gerçekten çalıştığınız kodu göstermediğiniz anlaşılıyor. 'Offset = m.end()' satırı, 'try'-' except' bloğunun içinde bulunur, bu nedenle hata işleyicinizin çıktısını almalısınız ve varsayılan traceback'i değil. –
Eh, tam çıkışı göstermiyorum. Çıktımın ilk iki satırı, geçersiz satırlar için hatayı gösterir, ancak bundan sonra özellik hatasını gösterir. – Chase
Geriye doğru oku. Bunu yorumlamanın sadece bir yolu var. Daha önce tracebackleri nasıl okuyacağınızı öğrendiniz mi? –