''int' object has no attribute 'items' error on dictionary

# Create the CSV file: csvfile
csvfile = open('crime_sampler.csv', 'r')

# Create a dictionary that defaults to a list: crimes_by_district
crimes_by_district = defaultdict(list)

# Loop over a DictReader of the CSV file
for row in csv.DictReader(csvfile):
    # Pop the district from each row: district
    district = row.pop('District')
    # Append the rest of the data to the list for proper district in crimes_by_district
    crimes_by_district[district].append(row)



# Loop over the crimes_by_district using expansion as district and crimes
for district, crimes in crimes_by_district.items():
    # Print the district
    print(district)

    # Create an empty Counter object: year_count
    year_count = Counter()

    # Loop over the crimes:
    for crime in crimes:
        # If there was an arrest
        if crime['Arrest'] == 'true':
            # Convert the Date to a datetime and get the year
            year = datetime.strptime(crime['Date'], '%m/%d/%Y %I:%M:%S %p').year
           # Increment the Counter for the year
            year_count += 1
        
    # Print the counter
    print(year_count)

I get an error:

AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_608/212322553.py in <cell line: 2>()
 14             year = datetime.strptime(crime['Date'], '%m/%d/%Y %I:%M:%S %p').year
 15             # Increment the Counter for the year
---> 16             year_count += 1
 17 
 18     # Print the counter
/usr/lib/python3.8/collections/__init__.py in __iadd__(self, other)
    814 
    815         '''
 --> 816         for elem, count in other.items():
    817             self[elem] += count
    818         return self._keep_positive()
    AttributeError: 'int' object has no attribute 'items'

I dont really catch why it occurred here, it's a dictinary but program says that it is integer It seems that i made a tricky mistake so i'll be very pleasant if you help me to find out that i did wrong



Solution 1:[1]

It seems you want to output year_count something like {"2010": 4, "2011": 5} ? The error is because year_count is not int

you can use a list

# Create an empty list object: year_count
    year_count = []

    # Loop over the crimes:
    for crime in crimes:
        # If there was an arrest
        if crime['Arrest'] == 'true':
            # Convert the Date to a datetime and get the year
            year = datetime.strptime(crime['Date'], '%m/%d/%Y %I:%M:%S %p').year
           # Increment the list for the year
            year_count.append(year)
        
    # Print the counter
    print(Counter(year_count))

alternatively you can use dictionary

# Create an empty list object: year_count
    year_count = dict()

    # Loop over the crimes:
    for crime in crimes:
        # If there was an arrest
        if crime['Arrest'] == 'true':
            # Convert the Date to a datetime and get the year
            year = datetime.strptime(crime['Date'], '%m/%d/%Y %I:%M:%S %p').year
           # Increment the dict for the year
            year_count[year] = year_count.get(year, 1) + 1
        
    # Print the counter
    print(year_count)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WSUN000