Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for循环内部的字符串连接非常慢_Python_String_Loops - Fatal编程技术网

Python for循环内部的字符串连接非常慢

Python for循环内部的字符串连接非常慢,python,string,loops,Python,String,Loops,我正在尝试创建xml输出,然后生成excel文件,在我的循环脚本上,它处理了数千个数据,但现在我有一百万个数据,需要太长时间,有时会挂断,我只是想知道是否有任何方法可以用来优化这里的循环 用法: 循环=10000工作正常 循环>=100000结果太慢且堆栈 class ExcelHyperlink: def __init__(self,sheetName,displayName): self.sheetName = sheetName.replace(" ",&q

我正在尝试创建xml输出,然后生成excel文件,在我的循环脚本上,它处理了数千个数据,但现在我有一百万个数据,需要太长时间,有时会挂断,我只是想知道是否有任何方法可以用来优化这里的循环

用法: 循环=10000工作正常

循环>=100000结果太慢且堆栈

class ExcelHyperlink:
  def __init__(self,sheetName,displayName):
    self.sheetName = sheetName.replace(" ","_")
    self.displayName = displayName
  def toCellString(self):
    sheet = escapeHTML(self.sheetName)
    display = escapeHTML(self.displayName)
    return '<Cell ss:StyleID="s63" ss:HRef="#%s!A1"><Data ss:Type="String">%s</Data></Cell>\n' % (sheet,display)    

def getCellString(value):
  if isinstance(value,ExcelHyperlink):
    return value.toCellString()
  else:
    return "<Cell><Data ss:Type=\"String\">%s</Data></Cell>\n" % (value)

loop = 10000
data = [{u'test': 0, u'a': 0, u'b': 0},{u'test': 1, u'a': 1, u'b': 1},{u'test': 2, u'a': 2, u'b': 2},{u'test': 3, u'a': 3, u'b': 3},{u'test': 4, u'a': 4, u'b': 4}] * loop

headers = ['test', 'a', 'b']


xml = '''<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"><WindowHeight>600</WindowHeight><WindowWidth>800</WindowWidth><WindowTopX>0</WindowTopX><WindowTopY>0</WindowTopY><ProtectStructure>False</ProtectStructure><ProtectWindows>False</ProtectWindows></ExcelWorkbook>
<Styles><Style ss:ID="Default" ss:Name="Normal"><Alignment ss:Vertical="Bottom"/><Borders/><Font/><Interior/><NumberFormat/><Protection/></Style><Style ss:ID="s21"><NumberFormat ss:Format="General Date"/></Style><Style ss:ID="s63" ss:Name="Hyperlink"><Font ss:Family="Arial" ss:Color="#0563C1" ss:Underline="Single"/></Style><Style ss:ID="s23"><Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/><Font x:Family="Swiss" ss:Bold="1"/></Style></Styles>
'''

for row in data:  
  xml += "<Row>\n"
  for item in headers:    
    #xml += str(row)
    xml += getCellString(row[item])
  xml += "</Row>\n"
xml += '''</Table>                
  
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Selected/>
<Panes></Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
'''
xml += "</Workbook>"
类超链接:
def uuu init uuuu(self、sheetName、displayName):
self.sheetName=sheetName.replace(“,”))
self.displayName=displayName
def toCellString(自):
sheet=escapeHTML(self.sheetName)
display=escapeHTML(self.displayName)
返回“%s\n%”(工作表,显示)
def getCellString(值):
如果isinstance(值、超链接):
返回值。toCellString()
其他:
返回“%s\n”%(值)
循环=10000
data=[{u'test':0,u'a':0},{u'test':1,u'a':1},{u'test':2,u'a':2,u'b':2},{u'test':3,u'b':3},{u'test':4,u'a':4}]*循环
标题=['test','a','b']
xml=“”
60080000False
'''
对于数据中的行:
xml+=“\n”
对于标题中的项目:
#xml+=str(行)
xml+=getCellString(行[项])
xml+=“\n”
xml+='''
假的
假的
'''
xml+=“”
python中的字符串是不可变的。所有这些重复的字符串添加操作都是浪费,因为每次迭代都必须分配和创建新的内存和对象,并复制数据

我建议将所有内容都放在一个列表中,并在最后调用
str.join

xml_artefacts = []
for row in data:  
    xml_artefacts.append("<Row>\n")
    for item in headers:    
        xml_artefacts.append(getCellString(row[item]))
    xml_artefacts.append("</Row>\n")

xml_artefacts.append('''</Table>  ...  </Workbook>''')
python中的字符串是不可变的。所有这些重复的字符串添加操作都是浪费,因为每次迭代都必须分配和创建新的内存和对象,并复制数据

我建议将所有内容都放在一个列表中,并在最后调用
str.join

xml_artefacts = []
for row in data:  
    xml_artefacts.append("<Row>\n")
    for item in headers:    
        xml_artefacts.append(getCellString(row[item]))
    xml_artefacts.append("</Row>\n")

xml_artefacts.append('''</Table>  ...  </Workbook>''')