Форум программистов, компьютерный форум, киберфорум
Python
Войти
Регистрация
Восстановить пароль
Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 5.00/25: Рейтинг темы: голосов - 25, средняя оценка - 5.00
В астрале
Эксперт С++
 Аватар для ForEveR
8049 / 4806 / 655
Регистрация: 24.06.2010
Сообщений: 10,562

Быстрая запись большого колличества данных на питоне

09.11.2011, 11:31. Показов 5104. Ответов 6
Метки нет (Все метки)

Студворк — интернет-сервис помощи студентам
Есть формиловалка ods документа через ooolib.
Есть у нас 500 к элементов. Допустим одинаковых строк вида
Python
1
some_str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
30 колонок. Количество строк - 500к / 30.
Только сохранение такой content.xml т.е. главной xml-ки с 500к элементами внутри + разные строки для корректного документа ods работает очень долго.
С использованием cStringIO работает 11 секунд для 500к элементов. Но все равно это медленно.
Пробовал формировать доку через lxml. Тоже медленно.
Есть-ли способы быстрее, кроме как писать свой модуль на Си?
0
IT_Exp
Эксперт
34794 / 4073 / 2104
Регистрация: 17.06.2006
Сообщений: 32,602
Блог
09.11.2011, 11:31
Ответы с готовыми решениями:

Быстрая запись из List в базу данных
Добрый день! На работе решили изобрести велосипед, я сделал, но он получился медленный. Суть дела: Предприятие решило запустить...

Запись в Эксель большого объема данных
Работаю с Excel через Apache POI(Чтение,запись,правка и тд). Для небольшого объема данных это не критично(поячеечное обращение к каждой...

VBS+MSSQL2008 - как ускорить запись большого количества данных в БД?
Доброго времени суток! В общем, есть задача записи в базу данных под mssql 2008 большого количества информации ,порядка 1.5млн строк....

6
224 / 209 / 63
Регистрация: 26.05.2011
Сообщений: 363
09.11.2011, 18:02
Может для начала cProfile натравить - будет видно, где тормозит...
ну и здесь код можно показать, наверняка будут советы по оптимизации
1
В астрале
Эксперт С++
 Аватар для ForEveR
8049 / 4806 / 655
Регистрация: 24.06.2010
Сообщений: 10,562
10.11.2011, 12:14  [ТС]
pyuser, Profile натравливал. Завтра скину вероятно результаты как на работе буду.

Добавлено через 14 часов 52 минуты
Для эксперимента возьму 100к элементов.

Первоначальный код, исходный из либы, который отвечает за приведение в xml форму, который и тормозит дико.

Код
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class XML:
    "XML Class - Used to convert nested lists into XML"
    def __init__(self):
        "Initialize ooolib XML instance"
        pass
 
    def _xmldata(self, data):
        datatype = data.pop(0)
        datavalue = data.pop(0)
        outstring = '%s' % datavalue
        return outstring
 
    def _xmltag(self, data):
        outstring = ''
        # First two
        datatype = data.pop(0)
        dataname = data.pop(0)
        outstring = '<%s' % dataname
        # Element Section
        element = 1
        while(data):
            # elements
            newdata = data.pop(0)
            if (newdata[0] == 'element' and element):
                newstring = self._xmlelement(newdata)
                outstring = '%s %s' % (outstring, newstring)
                continue
            if (newdata[0] != 'element' and element):
                element = 0
                outstring = '%s>' % outstring
                if (newdata[0] == 'tag' or newdata[0] == 'tagline'):
                    outstring = '%s\n' % outstring
            if (newdata[0] == 'tag'):
                newstring = self._xmltag(newdata)
                outstring = '%s%s' % (outstring, newstring)
                continue
            if (newdata[0] == 'tagline'):
                newstring = self._xmltagline(newdata)
                outstring = '%s%s' % (outstring, newstring)
                continue
            if (newdata[0] == 'data'):
                newstring = self._xmldata(newdata)
                outstring = '%s%s' % (outstring, newstring)
                continue
        if (element):
            element = 0
            outstring = '%s>\n' % outstring
        outstring = '%s</%s>\n' % (outstring, dataname)
        return outstring
 
    def _xmltagline(self, data):
        outstring = ''
        # First two
        datatype = data.pop(0)
        dataname = data.pop(0)
        outstring = '<%s' % dataname
        # Element Section
        while(data):
            # elements
            newdata = data.pop(0)
            if (newdata[0] != 'element'): break
            newstring = self._xmlelement(newdata)
            outstring = '%s %s' % (outstring, newstring)
        outstring = '%s/>\n' % outstring
        # Non-Element Section should not exist
        return outstring
 
    def _xmlelement(self, data):
        datatype = data.pop(0)
        dataname = data.pop(0)
        datavalue = data.pop(0)
        outstring = '%s="%s"' % (dataname, datavalue)   
        return outstring
 
    def convert(self, data):
        """Convert nested lists into XML
 
        The convert method takes a nested lists and converts them
        into XML to be used in Open Document Format documents.
        There are three types of lists that are recognized at this
        time.  They are as follows:
 
        'tag' - Tag opens a set of data that is eventually closed
        with a similar tag.
        List: ['tag', 'xml']
        XML: <xml></xml>
 
        'tagline' - Taglines are similar to tags, except they open
        and close themselves.
        List: ['tagline', 'xml']
        XML: <xml/>
 
        'element' - Elements are pieces of information stored in an
        opening tag or tagline.
        List: ['element', 'color', 'blue']
        XML: color="blue"
 
        'data' - Data is plain text directly inserted into the XML
        document.
        List: ['data', 'hello']
        XML: hello
 
        Bring them all together for something like this.
 
        Lists:
        ['tag', 'xml', ['element', 'a', 'b'], ['tagline', 'xml2'], 
        ['data', 'asdf']]
 
        XML:
        <xml a="b"><xml2/>asdf</xml>
        """
        outlines = []
        outlines.append('<?xml version="1.0" encoding="UTF-8"?>')
        if (type(data) == type([]) and len(data) > 0):
            if data[0] == 'tag':
                outlines.append(self._xmltag(data))
        return outlines


Вызывается он
Этим методом
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def _ods_content(self):
        "Generate ods content.xml data"
 
        # This will list all of the sheets in the document
        self.sheetdata = ['tag', 'office:spreadsheet']
        for sheet in self.sheets:
            if self.debug:  
                sheet_name = sheet.get_name()
                print "    Creating Sheet '%s'" % sheet_name
            sheet_list = sheet.get_lists()
            self.sheetdata.append(sheet_list)
        # Automatic Styles
        self.automatic_styles = self.styles.get_automatic_styles()
 
        self.data = ['tag', 'office:document-content',
          ['element', 'xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'],
          ['element', 'xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'],
          ['element', 'xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'],
          ['element', 'xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'],
          ['element', 'xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'],
          ['element', 'xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'],
          ['element', 'xmlns:xlink', 'http://www.w3.org/1999/xlink'],
          ['element', 'xmlns:dc', 'http://purl.org/dc/elements/1.1/'],
          ['element', 'xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'],
          ['element', 'xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'],
          ['element', 'xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'],
          ['element', 'xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0'],
          ['element', 'xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0'],
          ['element', 'xmlns:math', 'http://www.w3.org/1998/Math/MathML'],
          ['element', 'xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0'],
          ['element', 'xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0'],
          ['element', 'xmlns:ooo', 'http://openoffice.org/2004/office'],
          ['element', 'xmlns:ooow', 'http://openoffice.org/2004/writer'],
          ['element', 'xmlns:oooc', 'http://openoffice.org/2004/calc'],
          ['element', 'xmlns:dom', 'http://www.w3.org/2001/xml-events'],
          ['element', 'xmlns:xforms', 'http://www.w3.org/2002/xforms'],
          ['element', 'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema'],
          ['element', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'],
          ['element', 'office:version', '1.0'],
          ['tagline', 'office:scripts'],
          ['tag', 'office:font-face-decls',
            ['tagline', 'style:font-face',
              ['element', 'style:name', 'DejaVu Sans'],
              ['element', 'svg:font-family', '&apos;DejaVu Sans&apos;'],
              ['element', 'style:font-pitch', 'variable']],
            ['tagline', 'style:font-face',
              ['element', 'style:name', 'Nimbus Sans L'],
              ['element', 'svg:font-family', '&apos;Nimbus Sans L&apos;'],
              ['element', 'style:font-family-generic', 'swiss'],
              ['element', 'style:font-pitch', 'variable']]],
 
        # Automatic Styles
        self.automatic_styles,
 
          ['tag', 'office:body',
            self.sheetdata]]                                      # Sheets are generated from the CalcSheet class
 
        # Generate content.xml XML data
        xml = XML()
        self.lines = xml.convert(self.data)
        self.filedata = '\n'.join(self.lines)
        # Return generated data
        return self.filedata


В свою очередь у меня это вызывается через метод save, который для ods документа внутри себя использует переписанную save из ooolib (там не закрывался зип файл) - сохраняю на данный момент только content.xml.
Код
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def save(self, filename):
        """Save .ods spreadsheet.
 
        The save function saves the current cells and settings into a document.
        """
        if self.debug: print "Writing %s" % filename
        self.savefile = zipfile.ZipFile(filename, "w")
        #if self.debug: print "  meta.xml"
        #meta_save_time = datetime.datetime.utcnow()
        #self._zip_insert(self.savefile, "meta.xml", self.meta.get_meta())
        #print 'meta save time is: ', datetime.datetime.utcnow() - meta_save_time
        #if self.debug: print "  mimetype"
        #self._zip_insert(self.savefile, "mimetype", "application/vnd.oasis.opendocument.spreadsheet")
        #if self.debug: print "  Configurations2/accelerator/current.xml"
        #self._zip_insert(self.savefile, "Configurations2/accelerator/current.xml", "")
        #if self.debug: print "  META-INF/manifest.xml"
        #self._zip_insert(self.savefile, "META-INF/manifest.xml", self._ods_manifest())
        if self.debug: print "  content.xml"
        content_save_time = datetime.datetime.utcnow()
        self._zip_insert(self.savefile, "content.xml", self._ods_content())
        print 'content save time is: ', datetime.datetime.utcnow() - content_save_time
        #if self.debug: print "  settings.xml"
        #self._zip_insert(self.savefile, "settings.xml", self._ods_settings())
        #if self.debug: print "  styles.xml"
        #self._zip_insert(self.savefile, "styles.xml", self._ods_styles())
        # Add additional files if needed
        #for fileset in self.manifest_files:
            #(filename, filetype, newname) = fileset
            # Read in the file
            #data = self._file_load(filename)
            #if self.debug: print "  Inserting '%s' as '%s'" % (filename, newname)
            #self._zip_insert_binary(self.savefile, newname, data)
        self.savefile.close()


Результат. Без профайлера.

Code
1
content save time is:  0:01:10.617123
Профайлер вызывает просто дикое замедление, поэтому его стату выкладывать не буду.

Замена на cStringIO привела к такому коду. О правильности пока не заботился, поэтому работает он неверно, но мне важна пока только скорость.
Код

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from cStringIO import StringIO
class XML:
    "XML Class - Used to convert nested lists into XML"
    def __init__(self):
        self.IO = StringIO()
        "Initialize ooolib XML instance"
        pass
 
    def _xmldata(self, data):
        datatype = data.pop(0)
        datavalue = data.pop(0)
        self.IO.write('%s' + datavalue)
        #outstring = '%s' % datavalue
        #return outstring
 
    def _xmltag(self, data):
        outstring = ''
        # First two
        datatype = data.pop(0)
        dataname = data.pop(0)
        self.IO.write('<' + dataname)
        #outstring = '<%s' % dataname
        #print 'dname: ', dataname
        # Element Section
        element = 1
        while(data):
            # elements
            newdata = data.pop(0)
            if (newdata[0] == 'element' and element):
                self._xmlelement(newdata)
                #newstring = self._xmlelement(newdata)
                #outstring = '%s %s' % (outstring, newstring)
                #IO.write(' ' + newstring)
                continue
            if (newdata[0] != 'element' and element):
                element = 0
                self.IO.write('>')
                #outstring = '%s>' % outstring
                if (newdata[0] == 'tag' or newdata[0] == 'tagline'):
                    self.IO.write('\n')
                    #outstring = '%s\n' % outstring
            if (newdata[0] == 'tag'):
                self._xmltag(newdata)
                #newstring = self._xmltag(newdata)
                #outstring = '%s%s' % (outstring, newstring)
                continue
            if (newdata[0] == 'tagline'):
                self._xmltagline(newdata)
                #newstring = self._xmltagline(newdata)
                #outstring = '%s%s' % (outstring, newstring)
                continue
            if (newdata[0] == 'data'):
                self._xmldata(newdata)
                #newstring = self._xmldata(newdata)
                #outstring = '%s%s' % (outstring, newstring)
                continue
        if (element):
            element = 0
            self.IO.write('\n')
            #outstring = '%s>\n' % outstring
        self.IO.write('</' + dataname + '>\n')
        #outstring = '%s</%s>\n' % (outstring, dataname)
        #return outstring
 
    def _xmltagline(self, data):
        outstring = ''
        # First two
        datatype = data.pop(0)
        dataname = data.pop(0)
        self.IO.write('<' + dataname)
        #outstring = '<%s' % dataname
        # Element Section
        while(data):
            # elements
            newdata = data.pop(0)
            if (newdata[0] != 'element'): break
            #newstring = self._xmlelement(newdata)
            #outstring = '%s %s' % (outstring, newstring)
        self.IO.write('>\n')
        #outstring = '%s/>\n' % outstring
        # Non-Element Section should not exist
        #return outstring
 
    def _xmlelement(self, data):
        datatype = data.pop(0)
        dataname = data.pop(0)
        datavalue = data.pop(0)
        self.IO.write(' ' + dataname + '=' + datavalue)
        #outstring = '%s="%s"' % (dataname, datavalue)  
        #return outstring
 
    def convert(self, data):
        """Convert nested lists into XML
 
        The convert method takes a nested lists and converts them
        into XML to be used in Open Document Format documents.
        There are three types of lists that are recognized at this
        time.  They are as follows:
 
        'tag' - Tag opens a set of data that is eventually closed
        with a similar tag.
        List: ['tag', 'xml']
        XML: <xml></xml>
 
        'tagline' - Taglines are similar to tags, except they open
        and close themselves.
        List: ['tagline', 'xml']
        XML: <xml/>
 
        'element' - Elements are pieces of information stored in an
        opening tag or tagline.
        List: ['element', 'color', 'blue']
        XML: color="blue"
 
        'data' - Data is plain text directly inserted into the XML
        document.
        List: ['data', 'hello']
        XML: hello
 
        Bring them all together for something like this.
 
        Lists:
        ['tag', 'xml', ['element', 'a', 'b'], ['tagline', 'xml2'], 
        ['data', 'asdf']]
 
        XML:
        <xml a="b"><xml2/>asdf</xml>
        """
        outlines = []
        outlines.append('<?xml version="1.0" encoding="UTF-8"?>')
        if (type(data) == type([]) and len(data) > 0):
            if data[0] == 'tag':
                self._xmltag(data)
                outlines.append(self.IO.getvalue())
                self.IO.close()
        return outlines


Результат:
Code
1
content save time is:  0:00:01.941046
Неплохая скорость. Но недостаточная.

Так же был вариант юзать lxml.

Код
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    def _ods_content(self):
        "Generate ods content.xml data"
        # This will list all of the sheets in the document
        #self.sheetdata = []
        #self.sheetdata = ['tag', 'office:spreadsheet']
        # Automatic Styles
        NSMAP = {'office' : 'urn:oasis:names:tc:opendocument:xmlns:office:1.0',
             'style' : 'urn:oasis:names:tc:opendocument:xmlns:style:1.0',
             'text' : 'urn:oasis:names:tc:opendocument:xmlns:text:1.0',
             'table' : 'urn:oasis:names:tc:opendocument:xmlns:table:1.0',
             'draw' : 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0',
             'fo' : 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0',
             'xlink' : 'http://www.w3.org/1999/xlink',
             'dc' : 'http://purl.org/dc/elements/1.1/',
             'meta' : 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0',
             'number' : 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0',
             'svg' : 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0',
             'chart' : 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0',
             'dr3d' : 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0',
             'math' : 'http://www.w3.org/1998/Math/MathML',
             'form' : 'urn:oasis:names:tc:opendocument:xmlns:form:1.0',
             'script' : 'urn:oasis:names:tc:opendocument:xmlns:script:1.0',
             'ooo' : 'http://openoffice.org/2004/office',
             'ooow' : 'http://openoffice.org/2004/writer',
             'oooc' : 'http://openoffice.org/2004/calc',
             'dom' : 'http://www.w3.org/2001/xml-events',
             'xforms' : 'http://www.w3.org/2002/xforms',
             'xsd' : 'http://www.w3.org/2001/XMLSchema',
             'xsi' : 'http://www.w3.org/2001/XMLSchema-instance'}
        document_content_node = ET.Element('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-content',
                            nsmap = NSMAP)
        document_content_node.set('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}version', '1.0')
        ET.SubElement(document_content_node,  '{urn:oasis:names:tc:opendocument:xmlns:office:1.0}scripts')
        font_face_decls_node = ET.SubElement(document_content_node, '{urn:oasis:names:tc:opendocument:xmlns:office:1.0}font-face-decls')
        font_face_node1 = ET.SubElement(font_face_decls_node, '{urn:oasis:names:tc:opendocument:xmlns:style:1.0}font-face')
        font_face_node1.set('{urn:oasis:names:tc:opendocument:xmlns:style:1.0}name', 'DejaVu Sans')
        font_face_node1.set('{urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0}font-family', '&apos;DejaVu Sans&apos;')
        font_face_node1.set('{urn:oasis:names:tc:opendocument:xmlns:style:1.0}font-pitch', 'variable')
        font_face_node2 = ET.SubElement(font_face_decls_node, '{urn:oasis:names:tc:opendocument:xmlns:style:1.0}font-face')
        font_face_node2.set('{urn:oasis:names:tc:opendocument:xmlns:style:1.0}name', 'Nimbus Sans L')
        font_face_node2.set('{urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0}font-family', '&apos;Nimbus Sans L&apos;')
        font_face_node2.set('{urn:oasis:names:tc:opendocument:xmlns:style:1.0}font-family-generic', 'swiss')
        font_face_node2.set('{urn:oasis:names:tc:opendocument:xmlns:style:1.0}font-pitch', 'variable')
        self.styles.get_automatic_styles(document_content_node)
        body_node = ET.SubElement(document_content_node, '{urn:oasis:names:tc:opendocument:xmlns:office:1.0}body')
        spreadsheet_node = ET.SubElement(body_node, '{urn:oasis:names:tc:opendocument:xmlns:office:1.0}spreadsheet')
        for sheet in self.sheets:
            if self.debug:  
                sheet_name = sheet.get_name()
                print "    Creating Sheet '%s'" % sheet_name
            sheet.get_lists(spreadsheet_node)
        return ET.tostring(document_content_node, encoding = 'UTF-8', xml_declaration = True, pretty_print = False)


Результат.
Code
1
content save time is:  0:00:01.103054
Еще быстрее. Но жрет огромное колличество памяти и все равно недостаточно быстро.

В итоге даже при самом быстром варианте сохранение 500к элементов выливается в секунд 5-10.

Нужен же способ сохранять за приемлемое время около 3кк элементов.
0
224 / 209 / 63
Регистрация: 26.05.2011
Сообщений: 363
10.11.2011, 14:25
Детально разбираться в данный момент времени нет, но на первый взгляд - не верные структуры данных.
Python
1
2
3
4
5
from timeit import timeit
 
print(timeit("a=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"))
print(timeit("a=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)"))
#
на моем ПК результат такой:
0.255010843277
0.0358331203461

сделайте вывод

функции, возвращающие огромные списки, замените на генераторы
0
В астрале
Эксперт С++
 Аватар для ForEveR
8049 / 4806 / 655
Регистрация: 24.06.2010
Сообщений: 10,562
10.11.2011, 15:47  [ТС]
pyuser, Функции возвращающие огромные списки производятся на другом этапе этой самой либой. Т.е. ускорить сохранение возможно только полностью переписав всю либу?
0
224 / 209 / 63
Регистрация: 26.05.2011
Сообщений: 363
10.11.2011, 16:48
Цитата Сообщение от ForEveR Посмотреть сообщение
pyuser, Функции возвращающие огромные списки производятся на другом этапе этой самой либой. Т.е. ускорить сохранение возможно только полностью переписав всю либу?
Совершенно верно тормоза у Вас не на записи, а на генерации данных

ЗЫ. переписал Вашу функцию генерации xml - по скорости выигрыша практически нет, но выглядит более читаемо... и выдает валидный xml
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from сStringIO import StringIO
 
NODES = ('tag', 'xml', ('element', 'a', 'b'), ('element', 'c', 'd'), ('tagline', 'xml2'),
        ('tagline', 'xml3'), ('tagline', 'xml4'),
        ('data', 'asdf'), ('tag', 'xml', ('element', 'a', 'b'),
        ('tagline', 'xml2'), ('tagline', 'xml3'), ('tagline', 'xml4'),
        ('data', 'asdf')), ('tag', 'xml', ('element', 'a', 'b'),
        ('tagline', 'xml2'), ('tagline', 'xml3'), ('tagline', 'xml4'),
        ('data', 'asdf')))
FLAG = False
 
def make_xml(nodes):
    xmlnodes = {}
    out = StringIO()
    write = out.write
 
    write("<?xml version='1.0' encoding='UTF-8'?>")
 
    def tag(data):
        global FLAG
        if FLAG:
            write(">")
        write("<%s" % data[0])
        FLAG = True
        for node in data[1:]:
            xmlnodes[node[0]](node[1:])
        write("</%s>" % data[0])
 
    def tag_line(data):
        global FLAG
        if FLAG:
            write(">")
            FLAG = False
        write("<%s/>" % data[0])
 
    def element(data):
        write(" %s='%s'"% tuple(data))
 
    def data(data):
        global FLAG
        if FLAG:
            write(">")
            FLAG = False
        write("%s" % data[0])
 
    xmlnodes["tag"] = tag
    xmlnodes["tagline"] = tag_line
    xmlnodes["element"] = element
    xmlnodes["data"] = data
 
    xmlnodes[nodes[0]](nodes[1:])
 
    return(out.getvalue())
 
print(make_xml(NODES))
1
В астрале
Эксперт С++
 Аватар для ForEveR
8049 / 4806 / 655
Регистрация: 24.06.2010
Сообщений: 10,562
11.11.2011, 11:56  [ТС]
pyuser, Тормоза на записи... мм... да. есть такое дело. Спасибо за советы, быть может и перепишу под генераторы. Но это экономия на спичках насколько я понял. Все же настолько большие данные - поможет-ли?
0
Надоела реклама? Зарегистрируйтесь и она исчезнет полностью.
BasicMan
Эксперт
29316 / 5623 / 2384
Регистрация: 17.02.2009
Сообщений: 30,364
Блог
11.11.2011, 11:56
Помогаю со студенческими работами здесь

Запись большого объема данных в файл Word максимально быстро
вообщем есть массив данных...а точнее Datatable с обьемом порядка 200 000 строк. есть шаблон ворд. массив определенным образом...

Как в питоне взять интеграл от большого числа?
Добрый день! Помогите, пожалуйста! Нужно написать программу, которая находит число счастливых билетов с номерами, состоящими из N...

Быстрая сортировка большого массива классов (до миллиона элементов)
List&lt;PointResults&gt; resultValues = new List&lt;PointResults&gt;(); ... public class PointResults { public double Number { get;...

Передача большого объема данных (большого количества фотографий)
Есть веб-сервис ASP.NET, который работает по http протоколу. Однако есть необходимость передавать фотографии. передаю их в составе...

Быстрая запись в БД
Как записать в БД List по быстрее? Иcпользую метод - но для 300 записей уходит 20 секунд. ContentValues cv = new ContentValues(); ...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
7
Ответ Создать тему
Новые блоги и статьи
Хочу заставить корпорации вкладываться в здоровье сотрудников: делаю мат модель здравосохранения
anaschu 22.03.2026
e7EYtONaj8Y Z4Tv2zpXVVo https:/ / github. com/ shumilovas/ med2. git
1С: Программный отбор элементов справочника по группе
Maks 22.03.2026
Установка программного отбора элементов справочника "Номенклатура" из модуля формы документа. В качестве фильтра для отбора справочника служит группа номенклатуры. Отбор по наименованию группы. . .
Как я обхитрил таблицу Word
Alexander-7 21.03.2026
Когда мигает курсор у внешнего края таблицы, и нам надо перейти на новую строку, а при нажатии Enter создается новый ряд таблицы с ячейками, то мы вместо нервных нажатий Энтеров мы пишем любые буквы. . .
Krabik - рыболовный бот для WoW 3.3.5a
AmbA 21.03.2026
без регистрации и смс. Это не торговля, приложение не содержит рекламы. Выполняет свою непосредственную задачу - автоматизацию рыбалки в WoW - и ничего более. Однако если админы будут против -. . .
1С: Программный отбор элементов справочника по значению перечисления
Maks 21.03.2026
Установка программного отбора элементов справочника "Сотрудники" из модуля формы документа. В качестве фильтра для отбора служит значение перечислений. / / Событие "НачалоВыбора" реквизита на форме. . .
Переходник USB-CAN-GPIO
Eddy_Em 20.03.2026
Достаточно давно на работе возникла необходимость в переходнике CAN-USB с гальваноразвязкой, оный и был разработан. Однако, все меня терзала совесть, что аж 48-ногий МК используется так тупо: просто. . .
Оттенки серого
Argus19 18.03.2026
Оттенки серого Нашёл в интернете 3 прекрасных модуля: Модуль класса открытия диалога открытия/ сохранения файла на Win32 API; Модуль класса быстрого перекодирования цветного изображения в оттенки. . .
SDL3 для Desktop (MinGW): Рисуем цветные прямоугольники с помощью рисовальщика SDL3 на Си и C++
8Observer8 17.03.2026
Содержание блога Финальные проекты на Си и на C++: finish-rectangles-sdl3-c. zip finish-rectangles-sdl3-cpp. zip
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2026, CyberForum.ru