<p>前言:<br/>
我是Python新手,在windows7[32位]</strong>上使用pythonv3.3</em></em>。在stackoverflow上阅读了字典,以及如何将新的key<strong>:</strong>值对添加到新的或现有的字典中,我发现了一些有用的信息。但是,我还没有找到解决以下问题的方法。在</p>
<p><strong>[问题]:</strong><br/>
我想创建一个空字典,它将根据列表中元素的数量(从文件中的行读取)动态分配适当数量的键,并将值分配给相应的键。<br/><sub>注意:(列表中的元素是文件中每行的输出,用<strong>分隔\'</strong>和也是要分配给键的值)。</sub></p><hr/>
<p><strong>[工作区]:</strong></p>
<pre><code>#listMusic.py
import os; from fnmatch import fnmatch
# This works like a charm for producing a file with the output of the
# absolute paths we want to grab our list elements from.
def getWorkingPath(rootPath):
outputFile = open('output.txt', 'w')
filePattern = "*.mp3"
for path, subdirs, files in os.walk(rootPath):
for name in files:
if fnmatch(name, filePattern):
path_list = os.path.join(path, name + "\n")
outputData = outputFile.write(path_list)
outputFile.close()
# def setPathList():
'''continued in [Tried Section]'''
rootPath = getWorkingPath(input('Enter a path to crawl: '))<br>
</code></pre>
<p><strong>[工作运行]:</strong><br/><kbd>
C: \>;cd C:\Python33\Projects<br/>
C: \Python33\Projects>;列表音乐.py<br/>
输入要爬网的路径:“M:\Music”<br/>…<br/>
C: \Python33\Projects>;记事本输出.txt
</kbd><br/><br/>
<strong>[输出]:</strong><strong><em>输出.txt</em></strong><br/><sub>
M: \Music\Artist1\Album1\Track1.mp3<br/>
M: \Music\Artist1\Album1\Track2.mp3<br/>
M: \Music\Artist1\Album2\Track1.mp3<br/>
M: \Music\Artist1\Album2\Track2.mp3<br/>
M: \Music\Artist2\Album1\Track1.mp3<br/>
M: \Music\Artist2\Album1\Track2.mp3<br/>
M: \音乐\无序\Track1.mp3<br/>
M: \音乐\无序\Track2.mp3<br/>
<strong>…</strong><sub>(以此类推)</sub><strong>…</strong><br/></p><hr/>
<p><strong>[试过的部分]:</strong><br/></p>
^{pr2}$
<p>从函数<code>setPathList()</code>交互输入相同代码时,<br/>
输出如预期<em>(如果值列表中有5个元素…如果路径超出范围,则失败)</em>:<br/><br/>
<strong>[试运行]:</strong><br/>
<kbd>C:\>;Python<br/>
win32上的Python 3.3.2[MSC v.1600 32位(Intel)]<br/>
>;>></kbd></p>
<pre><code>for i in open(r'output.txt', 'r'):
v = i.rstrip("\n").split(os.sep)
k = {'drive':v[0], 'directory':v[1], 'artist':v[2],
'album':v[3], 'track':v[4]}
print(k)
</code></pre>
<p><kbd>…<br/>{drive':'M:','directory':'Music','Artist1','album':'Album1','track':'Track1.mp3'}<br/>
{'drive':'M:','directory':'Music','Artist1','album':'Album1','track':'Track2.mp3'}<br/>
{'drive':'M:','directory':'Music','Artist1','album':'Album2','track':'Track1.mp3'}<br/>
{'drive':'M:','directory':'Music','Artist1','album':'Album2','track':'Track2.mp3'}<br/>
{'drive':'M:','directory':'Music','Artist2','album':'Album1','track':'Track1.mp3'}<br/>
{'drive':'M:','directory':'Music','Artist2','album':'Album1','track':'Track2.mp3'}<br/>
{'drive':'M:','directory':'Music','Artist2','album':'Album2','track':'Track1.mp3'}<br/>
{'drive':'M:','directory':'Music','Artist2','album':'Album2','track':'Track2.mp3'}<br/><br/>
回溯(最近一次呼叫):<br/>
文件“[stdin]”,第3行,在[module]<br/>
索引器错误:列表索引超出范围<br/>
C: \>;</kbd><br/>
<br/>并且在运行模块时完全失败:<br/>
<kbd>C:\>;python-i列表音乐.py<br/>
>;>>打印(k)<br/>
回溯(最近一次呼叫):<br/>
文件“[stdin]”,第1行,在[module]<br/>
名称错误:名称“k”未定义<br/>
</kbd><br/><br/>
因此,我需要一种方法来创建一个字典,它创建与文件中的值相匹配的键,以及一种获取字典的正确方法。<br/></p><hr/>
<strong>[资源]:</strong><br/>
<a href="https://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array">How do I read a file line-by-line into a list?</a><br/>
<a href="https://stackoverflow.com/questions/3167154/how-to-split-a-dos-path-into-its-components-in-python">How to split a dos path into its components in Python</a><br/><hr/>
<strong>[最终想法]:</strong><br/>
我确信我可能遗漏了这个脚本中一些简单但重要的部分,以获得所需的输出。正如我所说,我是python的新手,这个脚本的大部分内容都是根据我对该语言的理解(松散/破碎)拼凑而成的。有答案的投票将是那些对产生结果的逻辑给出良好、连贯解释的投票。<br/>