从非空行获取值 XLSXWRITER(Python)

huangapple go评论74阅读模式
英文:

Get value from rows not null XLSXWRITER (python)

问题

我想合并第二列(带有“description”)与第一列(带有“name”):合并相同数量的行,但使用merge_range时第二列的值为空。

这是我的代码。

my_list = [
    {'field1': [
        {'name': 'value1', 'description': ''},
        {'name': 'value1', 'description': '这是描述...'},
        {'name': 'value1', 'description': ''},
    ]}
]
row = 206
for record in my_list:
    row_mem = row
    for rec in record['field1']:
        sheet.write(row, 2, rec['description'])
        row += 1
    sheet.merge_range(row_mem, 1, row - 1, 1, rec['name'])

结果:
从非空行获取值 XLSXWRITER(Python)

当将sheet.write(row, 2, rec['description'])替换为

sheet.merge_range(row_mem, 2, row - 1, 2, rec['description'])

我得到这个结果:
从非空行获取值 XLSXWRITER(Python)

但我想要显示如下:
从非空行获取值 XLSXWRITER(Python)

如何合并第二列并在合并时获取非空值。

请帮助一下,谢谢。

英文:

I want to merge column 2 (with "description") same to column 1(with "name") : same number of rows to merge , but when using merge_range I got null value in the column 2.

Here is my code.

 my_list = [
            {'field1': [
                {'name': 'value1', 'description': ""
                 },
                {'name': 'value1', 'description': "this is descr..."
                 },
                {'name': 'value1', 'description': ""
                 },

            ]
             },
        ]
        row = 206
        for record in my_list:
            row_mem = row
            for rec in record['field1']:
                sheet.write(row, 2, rec['description'])
                row += 1
            sheet.merge_range(row_mem, 1, row - 1, 1, rec['name'])

Result:
从非空行获取值 XLSXWRITER(Python)

when replacing sheet.write(row, 2, rec['description'])by

sheet.merge_range(row_mem, 2, row - 1, 2, rec['description'])

I got this result :
从非空行获取值 XLSXWRITER(Python)

But I want to display it like this:
从非空行获取值 XLSXWRITER(Python)

How I can merge column 2 and get the not null value when merging it .

Any help please ? Thanks.

答案1

得分: 1

你的代码示例中的缩进无效,我将假定行row = 206for record in my_list:以及for record ...之后的行与my_list=的缩进相同,并且与for record ...行保持相同的缩进。

根据你的代码,对于'description'列,要保留的值位于第二个单元格中,因此合并的三个单元格是空字符串、字符串值、空字符串。通常情况下,当单元格合并时,会保留左上角的单元格坐标和值,其他单元格中的值会丢失。因此,如果这三个单元格被合并,结果将是一个带有空字符串的单元格

然而,你的代码并没有像描述文本中那样合并单元格,而是通过以下方式实现的:

sheet.write(row, 2, rec['description'])

改成以下方式:

sheet.merge_range(row_mem, 2, row - 1, 2, rec['description'])

由于这意味着在每次循环中合并描述列,我预期在第一次循环中会收到一个warning,因为尝试在第一次循环中合并单个单元格,然后在第二次循环中合并两个单元格,即空字符串字符串值。因此,如上所述,单元格将合并并保留左上角的值,这是空字符串

不确定你的最终目标是什么,但我认为最好像对待'name'单元格一样合并你想要的单元格并保留值。

也许不需要循环,但假设你的列表my_list中有一些字段,那么像下面这样可能是更好的选择:

为了清晰起见,我添加了一个名为field2的第二个字段到my_list,原始字段名为field1。我使用了一个范围计数来跟踪行数。该范围从206开始,每次增加3,以便合并三行。范围的最大值是一个任意的数值。它不需要具体的值,只需要足够大以包含使用的行,my_list的循环将决定循环何时结束。

range(206, 1000, 3)

在第一个循环周期中,row被设置为206,然后根据行和设置的列1和2,将namedescription写入合并的单元格。在第二个循环周期中,row现在是209,然后根据该行和设置的列1和2,将下一个字段写入合并的单元格。

查看示例截图。

import xlsxwriter
    
book = xlsxwriter.Workbook('foo.xlsx')
sheet = book.add_worksheet('Sheet1')
    
my_list = [
    {'field1': [
        {'name': 'value1', 'description': "this is descr1..."},
    ]},
    {'field2': [
        {'name': 'value2', 'description': "this is descr2..."},
    ]}
]

### 使用zip将范围计数与字段数据组合在一起 
for (row, field) in zip(range(206, 1000, 3), my_list):
    for fname, values in field.items():
        sheet.merge_range(row, 1, row+2, 1, values[0]['name'])
        sheet.merge_range(row, 2, row+2, 2, values[0]['description'])
    
book.close()

给定示例的输出如下。

如果只有一个字段要添加,那么可以删除循环,只使用sheet.merge_range行就可以了。

英文:

The indent on your code sample is invalid, I'll assume that the lines row = 206 and for record in my_list: should be at the same indent as my_list= and the lines below for record ... retain the same indent in relation to that line.<br>
With your code as it is; for the &#39;description&#39; column the value to retain is in the 2nd cell, so the 3 cells being merged are empty string, string value, empty string. In usual case when cells are merged the top left cell coordinate and value are retained any values in other cells are lost. Therefore if these 3 cells are merged the result is a cell with empty string.<br>
<br>
However your code does not merge the cells as is, from the description text this is achieved by replacing

sheet.write(row, 2, rec[&#39;description&#39;])

with

sheet.merge_range(row_mem, 2, row - 1, 2, rec[&#39;description&#39;])

Since that means you are merging the description column on each loop I'd expect that'd give you a warning from attempting to merge a single cell on the first loop, then merge two cells, empty string & string value on the second loop. So as mentioned above the cells would merge retaining the top left value, this being the empty string.<br>
<br>
Not sure what your ultimate aim is but I think it's better you do as you have for the &#39;name&#39; cells and just merge together the cells you want with the value.<br>
The loop may not be required but assuming you have a few fields in the list mylist then something like this may be your better option.<br>

For clarity I have added a second field to my_list called field2 and the original is called field1. I have used a range count to track the rows. The range starts at 206 and steps by 3 for the three rows to be merged. The max value for the range is an arbitrary number. It doesn't need to be specific only large enough to encompass the rows used, the my_list loop will govern when the loop ends.

range(206, 1000, 3)

On 1st loop cycle the row is set to 206 and the name and description are written to merged cells based on the row and set columns 1 and 2. On the second loop cycle row is now 209 and the next field is written to merged cells based on that row and set columns 1 and 2.<br>
See the example screenshot.<br>
<br>

import xlsxwriter

book = xlsxwriter.Workbook(&#39;foo.xlsx&#39;)
sheet = book.add_worksheet(&#39;Sheet1&#39;)

my_list = [
            {&#39;field1&#39;: [
                {&#39;name&#39;: &#39;value1&#39;, &#39;description&#39;: &quot;this is descr1...&quot;},
            ]},
            {&#39;field2&#39;: [
                {&#39;name&#39;: &#39;value2&#39;, &#39;description&#39;: &quot;this is descr2...&quot;},
            ]}
]

### Use zip to combine range count with the field data 
for (row, field) in zip(range(206, 1000, 3), my_list):
    for fname, values in field.items():
        sheet.merge_range(row, 1, row+2, 1, values[0][&#39;name&#39;])
        sheet.merge_range(row, 2, row+2, 2, values[0][&#39;description&#39;])

book.close()

Output for given example<br>
从非空行获取值 XLSXWRITER(Python)

If you do only have the one field to add then remove the loop and just use the sheet.merge_range lines as they are.

huangapple
  • 本文由 发表于 2023年5月29日 04:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定