如何将public_client返回的Generator转为Pandas DataFrame并解决报错
Yep, that stray 'message' string at the end of your list is exactly what's causing the AttributeError! Here's why and how to fix it:
Why the Error Happens
pd.DataFrame.from_records() expects every element in your input to be a dictionary (or an object with a .keys() method) so it can map the keys to DataFrame columns. That random string 'message' doesn't have a .keys() attribute—hence the error you're seeing.
Fix: Filter Out Non-Dictionary Elements
You can solve this by filtering out any elements in the generator that aren't dictionaries before converting to a DataFrame. This is efficient to do directly with the generator (no need to convert it to a list first, which saves memory):
import pandas as pd # Filter the generator to keep only dictionary items filtered_trades = (item for item in data if isinstance(item, dict)) df = pd.DataFrame.from_records(filtered_trades)
If you prefer to first convert the generator to a list (to inspect it easily), you can filter the list instead:
import pandas as pd # Convert generator to list first trade_list = list(data) # Filter out non-dictionary elements filtered_trades = [item for item in trade_list if isinstance(item, dict)] df = pd.DataFrame(filtered_trades)
Bonus: Inspect the Mysterious 'message'
If you want to check what that 'message' string contains (it might be a warning or error from the API), you can extract it separately:
trade_list = list(data) # Split into valid trade dictionaries and other messages valid_trades = [item for item in trade_list if isinstance(item, dict)] api_messages = [item for item in trade_list if not isinstance(item, dict)] # Print the messages to debug print("API Messages:", api_messages) # Create DataFrame from valid trades df = pd.DataFrame(valid_trades)
内容的提问来源于stack exchange,提问作者spotnag

