示例#1
0
    def read_json(self):
        if not File.verify_file(self.file):
            print ('Read failed,file {} not exist!'.format(self.file))
        try:
            with open(self.file,'rt') as rf:
                return json.load(rf)

        except Exception as e:
            print ('Read json failed,error:{}'.format(e))
示例#2
0
    def read_csv(self):
        if not File.verify_file(self.file):
            print('Read failed,file {} not exist!'.format(self.file))

        rows = []
        try:
            with open(self.file, 'r', encoding='utf-8') as csvfile:
                csv_reader = csv.reader(csvfile)
                for index, row in enumerate(csv_reader):
                    if index == 0:
                        continue
                    rows.append(row)
                print('Read CSV file :{} ok!'.format(self.file))
                return rows

        except Exception as e:
            print('Read CSV file failed.Error:{}'.format(e))
            return None
示例#3
0
    def csv_to_xml(self, out_xml_file='movie.xml'):
        '''
        read the csv file content and convert it to xml file
        :param out_xml_file: 
        :return: 
        '''
        if not File.verify_file(self.file):
            print('Read failed,file {} not exist!'.format(self.file))
        else:
            with open(self.file, 'rt') as rf:
                reader = csv.reader(rf)
                if self.write_xml(reader, out_xml_file):
                    print('csv {} to xml {} ok!'.format(
                        self.file, out_xml_file))

                else:
                    print('csv {} to xml {} failed!'.format(
                        self.file, out_xml_file))