Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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 使用xlwgins在Excel中编写不具有相同列数的二维列表_Python_Excel_Xlwings - Fatal编程技术网

Python 使用xlwgins在Excel中编写不具有相同列数的二维列表

Python 使用xlwgins在Excel中编写不具有相同列数的二维列表,python,excel,xlwings,Python,Excel,Xlwings,我正在Excel工作簿中尝试编写如下文本文件: 14807 2010 20英寸-100英寸 0 关节 0 框架 3200 1 0 0 0“1”0“0 0.15.9 0”1.1 28 0 0.3.3 0 1 0 0 0 0“2”0“0 0.15.9 0”1.1 28 0 0.3.3 0 1 0 0 0 0“3”0“0 0.15.9 0”1.1 28 0 0.3.3 0 1 0 0 0 0 0 0 0 0 0.15.9 0 0 1.1 28 0 0.3 0 其思想是将每个值写入不同的单元格。我怎样才

我正在Excel工作簿中尝试编写如下文本文件:

14807 2010 20英寸-100英寸
0 关节 0 框架 3200 1 0 0 0“1”0“0 0.15.9 0”1.1 28 0 0.3.3 0
1 0 0 0 0“2”0“0 0.15.9 0”1.1 28 0 0.3.3 0
1 0 0 0 0“3”0“0 0.15.9 0”1.1 28 0 0.3.3 0
1 0 0 0 0 0 0 0 0 0.15.9 0 0 1.1 28 0 0.3 0

其思想是将每个值写入不同的单元格。我怎样才能有效地做到这一点?。我有这个但不起作用:

import xlwings as xw

filename = "Viguetas de N+47.00 a N+145.80.dcc"
dc_cad = DC_CAD(filename)
dc_cad.show_data()

class DC_CAD:
    def __init__(self, filename):
        self.filename = filename
        self.data = self.getData()
        print(self.data[:5])

        self.excel_app = xw.App(visible = False, add_book = False, impl = None)  # impl ?
        self.wb = self.excel_app.books.add()
        self.sh = self.wb.sheets[0]
        self.sh.range("A1").value = self.data

    def getData(self):
        return [line.split(' ') for line in open(self.filename, 'r').read().split('\n')]

    def show_data(self):
        self.excel_app.visible = True

我找到了这个解决方案,但速度非常慢:

class DC_CAD:
    def __init__(self, filename):
        self.filename = filename
        self.data = self.getData()

        self.excel_app = xw.App(visible = False, add_book = False, impl = None)  # impl ?
        self.wb = self.excel_app.books.add()
        self.sh = self.wb.sheets[0]
        rng = self.sh.range("A1")
        for i, line in enumerate(self.data[0:10]):
            rng.offset(i, 0).value = line

    def getData(self):
        return [' '.join(filter(None, line.split(' '))).split(' ') for line in open(self.filename, 'r').read().split('\n')]

    def show_data(self):
        self.excel_app.visible = True

使用CSV(逗号分隔值)格式大多数库(包括numpy和pandas)可能会处理它,但我需要继续使用Excel,那么,最好使用CSV库还是继续使用xlwigns?谢谢。您可以在Excel中使用csv和csv库。是否可以在每个单元格中保存每个值?