擅长:python、mysql、java
<p>只需对代码进行最少的修改:</p>
<pre><code>def isbn_dictionary(filename):
"""docstring"""
file = open(filename, "r")
library = {}
for line in file:
line = line.split(",")
if line[0]: # Only append if there is a value in the first column
tup = (line[0], line[1])
library[line[2].strip()] = tup # get rid of newlines in the key
file.close() # It's good practice to always close the file when done. Normally you'd use "with" for handling files.
return library
print(isbn_dictionary("books.csv"))
</code></pre>
<p>空字符串是<em>false</em>,因此如果行的第一个条目是空的,则这不会添加到<code>library</code>dict中。你知道吗</p>