Python 2.7报错:'list'对象无'get'方法,CSV写入卡壳求助
It sounds like the root of your CSV writing problem is that you're trying to use the get() method on something that isn't a dictionary—most likely a tuple from iteritems() (or items() in Python 3). Let's break this down and fix it step by step.
Common Mistake: Treating Tuple Pairs as Dictionaries
When you loop through some_dict.iteritems() (Python 2) or some_dict.items() (Python 3), you get an iterator of (key, value) tuples. These tuples don't have a get() method—only dictionaries do. So if you're trying to do something like item.get('address') where item is one of these tuples, you'll get an error.
Example of the Wrong Approach
Suppose your scraped data looks like this (a dictionary of courts, where keys are names and values are address details):
courts = { "Manchester Crown Court": {"address": "Spinningfields, Manchester M3 3HH"}, "Birmingham Magistrates' Court": {"address": "Victoria Law Courts, Birmingham B1 3JR"} }
If you try writing to CSV like this, it will fail:
import csv with open("courts.csv", "w") as f: writer = csv.DictWriter(f, fieldnames=["court_name", "address"]) writer.writeheader() for item in courts.iteritems(): # This gives tuples like ("Manchester Crown Court", {...}) # Trying to call get() on the tuple will throw an error! writer.writerow({"court_name": item.get("name"), "address": item.get("address")})
Corrected Approach
Instead, unpack the tuple into separate variables for the key and value, then use the value (which is a dictionary) to access the address:
import csv courts = { "Manchester Crown Court": {"address": "Spinningfields, Manchester M3 3HH"}, "Birmingham Magistrates' Court": {"address": "Victoria Law Courts, Birmingham B1 3JR"} } with open("courts.csv", "w", newline="") as f: writer = csv.DictWriter(f, fieldnames=["court_name", "address"]) writer.writeheader() # Unpack each (name, details) tuple from the dictionary for court_name, details in courts.items(): # Use items() for Python 3, iteritems() for Python 2 # Access the address using details.get() (since details is a dict) row = { "court_name": court_name, "address": details.get("address", "") # Use get() to handle missing addresses gracefully } writer.writerow(row)
If You're Using a List of Dictionaries
If your scraped data is a list of dictionaries (each representing one court), you can loop through the list directly—no need for iteritems() at all:
import csv courts = [ {"court_name": "Manchester Crown Court", "address": "Spinningfields, Manchester M3 3HH"}, {"court_name": "Birmingham Magistrates' Court", "address": "Victoria Law Courts, Birmingham B1 3JR"} ] with open("courts.csv", "w", newline="") as f: writer = csv.DictWriter(f, fieldnames=["court_name", "address"]) writer.writeheader() for court in courts: # court is a dictionary, so get() works here writer.writerow({ "court_name": court.get("court_name", ""), "address": court.get("address", "") }) # Or just pass the court dict directly if it has exactly the fieldnames: # writer.writerow(court)
Key Takeaways
- Stop using
iteritems()on the wrong object: If you're working with a list of courts, loop through the list elements (each should be a dict). If you're working with a dict of courts, unpack the (key, value) tuples first. - Only call
get()on dictionaries: Make sure the variable you're usingget()on is actually a dictionary (like thedetailsvariable in the first corrected example). - Use
items()for Python 3:iteritems()was removed in Python 3—replace it withitems().
This should resolve the missing get() method error and let you write your court data to CSV successfully.
内容的提问来源于stack exchange,提问作者ron_g

