Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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 附加到关联数组_Python_Python 3.x - Fatal编程技术网

Python 附加到关联数组

Python 附加到关联数组,python,python-3.x,Python,Python 3.x,我有一个Python脚本,它可以遍历PDF文件(在每个页面上循环),并在每个页面内执行一些文本操作。所以基本上有两个循环: files = {} #npages is the number of PDF pages in the specific file. for n in range(npages): path = pdf_name + str(n + 1) + '_1.txt' files[int(n)] = path for i, col in enum

我有一个Python脚本,它可以遍历PDF文件(在每个页面上循环),并在每个页面内执行一些文本操作。所以基本上有两个循环:

files = {}

#npages is the number of PDF pages in the specific file.

for n in range(npages):

    path = pdf_name + str(n + 1) + '_1.txt'

    files[int(n)] = path

    for i, col in enumerate(COLUMNS):

        path = pdf_name + str(n + 1) + '_' + str(i + 2) + '.txt'
        files[int(n)][int(i)] = path
基本上,我查看每个PDF页面,然后在每个页面上进一步执行一些文本操作

我试图将其输出为:

- file_page_1.pdf
  - file_page_1_col_1.pdf
  - file_page_1_col_2.pdf
file_page_2.pdf
  - file_page_2_col_1.pdf
  - file_page_2_col_2.pdf
但是,使用上述COE会产生以下错误:

files[int(n)][int(i)] = path
TypeError: 'str' object does not support item assignment

这是因为
files[int(n)]
返回的是
str而不是字典

从你的台词中可以看出

files[int(n)] = path
您正试图从
str
对象实现字典行为。 为了实现你想做的事情,我们可以做一些类似的事情

from collections import defaultdict

files = {}
for n in range(npages):
    path = pdf_name + str(n + 1) + '_1.txt'
    files[int(n)] = defaultdict()
    files[int(n)]['path_root'] = path

    for i, col in enumerate(COLUMNS):
        path = pdf_name + str(n + 1) + '_' + str(i + 2) + '.txt'
        files[int(n)][int(i)] = path
这将为您提供如下结果:

|-- nth file 
|    |
|    |- path_root
|    |- child1 (0)
|    |- child2 (1)
..
关于
defaultdict
的简短旁注:

somedict = {}
print(somedict[3]) # KeyError

someddict = defaultdict(int) # or str
print(someddict[3]) # print int(), thus 0 (str will return you '')

我认为您正在寻找的结构是一个dict,它有字符串键来列出值

files = {}

for page in range(npages):
    path = pdf_name + str(n+1) + '_1.txt'
    files[path] = []
    for i, col in enumerate(COLUMNS):
        subpath = pdf_name + str(n + 1) + '_' + str(i + 2) + '.txt'
        files[path].append(subpath)

# For accessing items
for path, subpaths in files.items():
    # path is a string, the key in files dict
    print(path) 
    # subpaths is a list of strings, the value in files dict
    for subpath in subpaths:
        print(subpath)
如果希望按照插入的顺序返回路径/子路径对,可以使用OrderedDict而不是dict

from collections import OrderedDict
files = OrderedDict()
# code as above

基本上,我“只是”尝试创建一个关联数组,其中页面路径是主数组,然后该页面上的“子文件”就是子数组。您应该尝试
files[int(n)]({int(I):path})
@P.hunter,它给我
'str'对象是不可调用的
不清楚您想要什么。你能举一个最简单的例子来说明你想要的结果吗?
文件应该是什么样的?使用
print(files)
我们应该看到什么。为什么要按
n
索引
文件
,但所需的输出不包括这些
n
?它看起来更像是希望您的
文件
有一个
路径
的键,并且该值是一个子路径列表?然后,您可以在第一个循环中执行
files[path]=[]
,然后在第二个循环中执行
subpath=pdf\u name+…
files[path]。追加(subpath)
。(不重用变量名
path
,因为您需要原始路径名来索引dict)这给了我一个错误
sub_path[int(n)]=defaultdict()name错误:如果我将其更改为
path
,则未定义名称“sub_path”
,我仍然收到与以前相同的错误:
'str'对象不支持项目分配
不用担心,谢谢您的帮助!它几乎可以工作,但是第一个
路径
没有添加到列表中。这一条:
path=pdf\u name+str(n+1)+'\u 1.txt'
查看new edit@oliverbj,如果有任何问题,请告诉我。我看不到您在第一个循环
中为n in-range(npages)
添加
路径。您仅在此处创建defaultdict。我需要两个循环的路径