Python 使用嵌套循环创建键值列表

Python 使用嵌套循环创建键值列表,python,python-3.x,python-3.7,Python,Python 3.x,Python 3.7,所以我有一个脚本,它检查PDF文件中的每一页,然后在每一页上,将PDF文件的文本划分为列 考虑以下列: {"1":{"position":"15"}, "2":{"position": "50"}}' pages={} npages=2#PDF中的页数。 对于范围内的n(n页): 页面[n+1]=[] 对于i,枚举中的列(列): out=“第n页第1列文本第2列文本”#简化字符串。 页[n+1]。追加({int(i+1):str(out)}) 我的假设是,这将创建一个键值对,如: page

所以我有一个脚本,它检查PDF文件中的每一页,然后在每一页上,将PDF文件的文本划分为列

考虑以下列:

{"1":{"position":"15"}, "2":{"position": "50"}}'
pages={}
npages=2#PDF中的页数。
对于范围内的n(n页):
页面[n+1]=[]
对于i,枚举中的列(列):
out=“第n页第1列文本第2列文本”#简化字符串。
页[n+1]。追加({int(i+1):str(out)})
我的假设是,这将创建一个键值对,如:

page n: text inside the column
但出于某种原因,上面的脚本创建了一对,如下所示:

{1: 'Page 1 Column 1 Text'} - {2: 'Page 1 Column 2 Text'}
{1: 'Page 2 Column 1 Text'} - {2: 'Page 2 Column 2 Text'}
如您所见,它创建的关键点如下:

{1: 'Page 1 Column 1 Text'}
假设我想这样做:(输出值用于第一次迭代)

对于页面,页面中的列。值()
打印(“页面:{}”。格式(页面))#应输出:页面:1
打印(“列文本:{}”。格式(列))#应输出:列文本:列1文本
总之,我想要的输出是(其中页码是键,列文本是值):

{1:“第1页第1列文本”}
{1:'第2列文本'}
{2:'第2页第1列文本'}
{2:'第2列文本'}

我错过了什么?如果这是基本的,我很抱歉,我是Python新手。

看起来您只是想要一个页面列表:

pages = []
npages = 2  # Number of pages in the PDF.
COLUMNS = ["example1", "example2", "example3"]
for n in range(npages):

    for i, col in enumerate(COLUMNS):
        if i == 0:
            pages.append({n + 1: "Page {} Column {} {}".format(n + 1, i + 1, col)})
        else:
            pages.append({n + 1: "Column {} {}".format(i + 1, col)})
然后,页面将被定义为:

[{1: 'Page 1 Column 1 example1'},
 {1: 'Column 2 example2'},
 {1: 'Column 3 example3'},
 {2: 'Page 2 Column 1 example1'},
 {2: 'Column 2 example2'},
 {2: 'Column 3 example3'}]
{
    'Page 1': {
        'Column 1': 'example1',
        'Column 2': 'example2',
        'Column 3': 'example3'
    },
    'Page 2': {
        'Column 1': 'example1',
        'Column 2': 'example2',
        'Column 3': 'example3'
    }
}

根据评论更新: 列表并不是以这种方式解析内容的理想方式——如果您试图访问每个页面的列内容,那么一个dict of dict将更有意义。例如:

pages = {}
npages = 2  # Number of pages in the PDF.
COLUMNS = ["example1", "example2", "example3"]
for n in range(npages):
    page_name = "Page {}".format(n + 1)
    pages[page_name] = {}
    for i, col in enumerate(COLUMNS):
        column_name = "Column {}".format(i + 1)
        pages[page_name][column_name] = col
结果页面定义为:

[{1: 'Page 1 Column 1 example1'},
 {1: 'Column 2 example2'},
 {1: 'Column 3 example3'},
 {2: 'Page 2 Column 1 example1'},
 {2: 'Column 2 example2'},
 {2: 'Column 3 example3'}]
{
    'Page 1': {
        'Column 1': 'example1',
        'Column 2': 'example2',
        'Column 3': 'example3'
    },
    'Page 2': {
        'Column 1': 'example1',
        'Column 2': 'example2',
        'Column 3': 'example3'
    }
}

看起来你只是想要一个页面列表:

pages = []
npages = 2  # Number of pages in the PDF.
COLUMNS = ["example1", "example2", "example3"]
for n in range(npages):

    for i, col in enumerate(COLUMNS):
        if i == 0:
            pages.append({n + 1: "Page {} Column {} {}".format(n + 1, i + 1, col)})
        else:
            pages.append({n + 1: "Column {} {}".format(i + 1, col)})
然后,页面将被定义为:

[{1: 'Page 1 Column 1 example1'},
 {1: 'Column 2 example2'},
 {1: 'Column 3 example3'},
 {2: 'Page 2 Column 1 example1'},
 {2: 'Column 2 example2'},
 {2: 'Column 3 example3'}]
{
    'Page 1': {
        'Column 1': 'example1',
        'Column 2': 'example2',
        'Column 3': 'example3'
    },
    'Page 2': {
        'Column 1': 'example1',
        'Column 2': 'example2',
        'Column 3': 'example3'
    }
}

根据评论更新: 列表并不是以这种方式解析内容的理想方式——如果您试图访问每个页面的列内容,那么一个dict of dict将更有意义。例如:

pages = {}
npages = 2  # Number of pages in the PDF.
COLUMNS = ["example1", "example2", "example3"]
for n in range(npages):
    page_name = "Page {}".format(n + 1)
    pages[page_name] = {}
    for i, col in enumerate(COLUMNS):
        column_name = "Column {}".format(i + 1)
        pages[page_name][column_name] = col
结果页面定义为:

[{1: 'Page 1 Column 1 example1'},
 {1: 'Column 2 example2'},
 {1: 'Column 3 example3'},
 {2: 'Page 2 Column 1 example1'},
 {2: 'Column 2 example2'},
 {2: 'Column 3 example3'}]
{
    'Page 1': {
        'Column 1': 'example1',
        'Column 2': 'example2',
        'Column 3': 'example3'
    },
    'Page 2': {
        'Column 1': 'example1',
        'Column 2': 'example2',
        'Column 3': 'example3'
    }
}

@Sanyash我已经更新了我的问题-如果有点不清楚,很抱歉!你不想要
pages[n+1].append({int(n+1):str(out)})
吗?那么你想要每列有一个不同的字典吗?@BillF不,我想要
列索引
嵌套在
页索引
下。例如,在第1页,我定义了两列。例如,我希望能够打印:在第
1
页上,我有
columntext1
columntext2
@smac89列,我不确定它是否是另一本词典?我正在尝试创建一个包含页面索引和列索引的关联数组。@Sanyash我已经更新了我的问题-如果有点不清楚,很抱歉!你不想要
pages[n+1].append({int(n+1):str(out)})
吗?那么你想要每列有一个不同的字典吗?@BillF不,我想要
列索引
嵌套在
页索引
下。例如,在第1页,我定义了两列。例如,我希望能够打印:在第
1
页上,我有
columntext1
columntext2
@smac89列,我不确定它是否是另一本词典?我正在尝试创建一个关联数组,包含页面索引和列索引。您在更新的答案中发布的代码正是我想要实现的!非常感谢你!你在更新答案中发布的代码正是我想要实现的!非常感谢你!