问题描述:
Pandas无法将数据框转换为csv文件,返回错误信息:AttributeError: 'str' object has no attribute 'to_csv'
尝试用trends.to_string(index=False).to_csv('test.csv'))
等其他一些例子,依然还是提示同样错误。
def main(url):
google = GoogleAnalysis(url)
codes = country_codes()
return pd.concat([
google.trends(country_code)
for country_code in codes[:len(codes) // 2]
])
def trends(self,country_code):
df = pd.DataFrame(
self._retrieve_trends(country_code),
columns=['Title', 'Search_Score'],
)
df['Country Code'] = country_code
df['Platform'] = 'Google'
return df
if __name__ == '__main__':
trends = main('https://trends.google.com/trends/trendingsearches/daily/rss')
trends.to_csv('k.csv').to_string(index=False)
问题解决:
需要输入trends
函数的参数:
def trends(self,country_code):
df = pd.DataFrame(
self._retrieve_trends(country_code),
columns=['Title', 'Search_Score'],
)
df['Country Code'] = country_code
df['Platform'] = 'Google'
return df
if __name__ == '__main__':
trends = main(
'https://trends.google.com/trends/trendingsearches/daily/rss')
trends.to_csv('k.csv')








暂无数据