#!/usr/bin/env python # -*- coding: utf-8 -*- def ParseByLines(file_path): """ Чтение и анализ содержимого файла Входные данные: file_path - путь к файлу """ # словари с библиотеками, идентификатора, операторами и т.д. libraries={} operators={} variables={} class_var={} functions={} comments =0 ## открываем файл и читаем его построчно file=open(file_path,"r","utf-8") for line in file.readlines(): string=line.replace('\n','') # разбор строки: for index in string: # Комментарии if(string[index]=="#"): commets+=1 break; print("Комментариев в файле: %d"%(comments)) file.close() if __name__ == '__main__': import os # чтение файлов из каталога #directory = "C:\\Code\\Python\\test\\" #files = os.listdir(directory); #for file in files: # file=open(...) # print(line,end='') # ... # file.close() ParseByLines("C:\\Code\\Python\\test\\test.rb")
Почему выдается ошибку вида:
Traceback (most recent call last):
File “C:\Python\lib\encodings\cp1251.py”, line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)
UnicodeDecodeError: ‘charmap’ codec can't decode byte 0x98 in position 1130: character maps to <undefined>