拆分代理文本文件时遭遇TypeError:int与list无法执行+运算
Fixing TypeError: unsupported operand type(s) for +: 'int' and 'list' in Proxy Splitter Program
First off, that error pops up because somewhere in your code you’re trying to add an integer (like a count of proxies) to a list (like a group of proxies)—Python doesn’t let you mix those types with the + operator. Let’s walk through fixing this and getting your proxy splitter working properly.
Here’s a complete, corrected version of the program that avoids this error and does exactly what you need:
filename = input("Enter a file name: ") # Load all proxies from the file into a clean list (skip empty lines) with open(filename) as f: proxies = [line.strip() for line in f if line.strip()] total_proxies = len(proxies) print(f"Loaded {total_proxies} proxies successfully.") # Get split rules from the user print("\nEnter split rules (e.g., '5x10,2x25' for 5 groups of 10, 2 groups of 25):") split_input = input().strip() # Parse the input into usable split rules split_rules = [] try: for part in split_input.split(','): num_groups, group_size = part.split('x') split_rules.append((int(num_groups), int(group_size))) except ValueError: print("Invalid input format! Use 'NxM' separated by commas (e.g., 5x10,2x25)") exit() # Verify the split rules match the total number of proxies required_total = sum(num * size for num, size in split_rules) if required_total != total_proxies: print(f"Error: Split rules need {required_total} proxies, but your file has {total_proxies}.") exit() # Split proxies and write to output files current_position = 0 file_number = 1 for num_groups, group_size in split_rules: for _ in range(num_groups): # Slice the proxy list to get the current group current_group = proxies[current_position:current_position + group_size] # Write the group to a new file with open(f"proxy_group_{file_number}.txt", 'w') as out_file: out_file.write('\n'.join(current_group)) print(f"Created proxy_group_{file_number}.txt with {len(current_group)} proxies.") # Move to the next set of proxies current_position += group_size file_number += 1
Key fixes that prevent the TypeError:
- We first load all proxies into a list
proxiesand calculate the total usinglen(proxies)(a pure integer, no mixing of types). - When validating the split rules, we sum integers only:
sum(num * size for num, size in split_rules)—no attempts to add lists to counts. - Splitting uses list slicing to grab each group, which keeps operations on lists and integers separate.
How to use it:
- Save your proxies in a text file (one proxy per line).
- Run the program, enter the filename when prompted.
- Input your split rules in the format
NxMseparated by commas (like5x10,2x25for your example). - The program will generate numbered text files with your split proxy groups.
内容的提问来源于stack exchange,提问作者user9680749
相关产品推荐
相关产品推荐

