这段代码中出现KeyError: 'username'
时间: 2024-12-20 20:19:48 浏览: 8
在您提供的代码片段中,`KeyError: 'username'` 错误可能出现在 `add_data` 函数中的某些部分,因为并不是所有的字典都包含了 `'username'` 这个键。具体来说,以下这些数据结构没有 `'username'`:
- `accounts_data`
- `movies_data`
- `series_data`
- `seasons_data`
- `episodes_data`
- `games_data`
- `has_g_data`
- `avail_language_data`
- `game_type_data`
- `cars_data`
- `weapons_data`
- `check_point_data`
为了防止这种错误,可以在 `add_data` 函数中检查每个数据结构是否包含 `'username'` 键,然后再进行操作。以下是修改后的 `add_data` 函数:
```python
def add_data(data, num=100):
for i in range(num):
if 'email' in data:
data['email'].append(f'user{i+3}@example.com')
if 'username' in data:
data['username'].append(f'user{i+3}')
if 'password' in data:
data['password'].append(f'password{i+3}')
if 'primary_ip_addr' in data:
data['primary_ip_addr'].append(f'192.168.1.{i+3}')
if 'plan_id' in data:
data['plan_id'].append((i % 2) + 1)
if 'year' in data:
data['year'].append(2023)
if 'month' in data:
data['month'].append(10)
if 'day' in data:
data['day'].append(i % 28 + 1)
if 'duration' in data:
data['duration'].append((i % 60) + 1)
if 'p_location' in data:
data['p_location'].append(['New York', 'Los Angeles'][i % 2])
if 'log_id' in data:
data['log_id'].append(i + 3)
if 'device_id' in data:
data['device_id'].append((i % 2) + 1)
if 'watching' in data:
data['watching'].append(bool(i % 2))
if 'downloading' in data:
data['downloading'].append(not bool(i % 2))
if 'ip_addr' in data:
data['ip_addr'].append(f'192.168.1.{i+3}')
if 'location' in data:
data['location'].append(['New York', 'Los Angeles'][i % 2])
if 'timestamp' in data:
data['timestamp'].append(f'2023-10-{i%28+1}T12:00:00')
if 'icon' in data:
data['icon'].append(f'icon{i+3}')
if 'e_primary_ip' in data:
data['e_primary_ip'].append(f'192.168.1.{i+3}')
if 'e_location' in data:
data['e_location'].append(['New York', 'Los Angeles'][i % 2])
if 'movie_id' in data:
data['movie_id'].append((i % 2) + 1)
if 'rate' in data:
data['rate'].append(4.0 - (i % 2) * 0.5)
if 'series_id' in data:
data['series_id'].append((i % 2) + 1)
if 'sea_id' in data:
data['sea_id'].append((i % 2) + 1)
if 'epi_id' in data:
data['epi_id'].append((i % 2) + 1)
if 'movie_name' in data:
data['movie_name'].append(f'Movie{i+3}')
if 'description' in data:
data['description'].append(f'Description of Movie{i+3}')
if 'duration' in data and 'movie_id' not in data:
data['duration'].append((i % 60) + 60)
if 'p_year' in data:
data['p_year'].append(2020 + (i % 2))
if 'p_month' in data:
data['p_month'].append(1)
if 'e_year' in data:
data['e_year'].append(2021 + (i % 2))
if 'e_month' in data:
data['e_month'].append(2)
if 'e_day' in data:
data['e_day'].append(1)
if 'r_year' in data:
data['r_year'].append(2021 + (i % 2))
if 'r_month' in data:
data['r_month'].append(3)
if 'r_day' in data:
data['r_day'].append(1)
if 'director_id' in data:
data['director_id'].append((i % 2) + 1)
if 'series_name' in data:
data['series_name'].append(f'Series{i+3}')
if 'sea_name' in data:
data['sea_name'].append(f'Season{i+3}')
if 'epi_name' in data:
data['epi_name'].append(f'Episode{i+3}')
if 'game_id' in data:
data['game_id'].append((i % 2) + 1)
if 'game_name' in data:
data['game_name'].append(f'Game{i+3}')
if 'online' in data:
data['online'].append(bool(i % 2))
if 'controller_support' in data:
data['controller_support'].append(bool(i % 2))
if 'publish_year' in data:
data['publish_year'].append(2020 + (i % 2))
if 'language' in data:
data['language'].append(['en', 'es', 'fr'][i % 3])
if 'type' in data:
data['type'].append(['Action', 'Adventure', 'RPG'][i % 3])
if 'car_id' in data:
data['car_id'].append((i % 2) + 1)
if 'car_name' in data:
data['car_name'].append(f'Car{i+3}')
if 'price' in data:
data['price'].append((i % 1000) + 1000)
if 'value' in data:
data['value'].append((i % 500) + 500)
if 'weapon_id' in data:
data['weapon_id'].append((i % 2) + 1)
if 'weapon_name' in data:
data['weapon_name'].append(f'Weapon{i+3}')
if 'quality' in data:
data['quality'].append((i % 100) + 100)
if 'additional_ability' in data:
data['additional_ability'].append(bool(i % 2))
if 'health' in data:
data['health'].append((i % 100) + 100)
if 'coin' in data:
data['coin'].append((i % 500) + 500)
if 'cp_id' in data:
data['cp_id'].append((i % 2) + 1)
# 调用函数添加数据
add_data(accounts_data)
add_data(login_log_data)
add_data(users_data)
add_data(watch_m_data)
add_data(watch_s_data)
add_data(movies_data)
add_data(series_data)
add_data(seasons_data)
add_data(episodes_data)
add_data(games_data)
add_data(download_data)
add_data(has_g_data)
add_data(avail_language_data)
add_data(game_type_data)
add_data(belong_g_data)
add_data(cars_data)
add_data(weapons_data)
add_data(check_point_data)
add_data(own_c_data)
add_data(own_w_data)
add_data(resources_data)
```
通过这种方式,可以避免在不包含 `'username'` 键的数据结构上调用该键时引发 `KeyError`。
阅读全文