Python 3.x 将CSV文件转换为Excel
我正试图抓取一个网站并将其输出到Excel文件中。我设法创建了Excel文件,但列都乱了。请看图片。 如何将数据从CSV文件正确传输到Excel文件 我使用的代码是:Python 3.x 将CSV文件转换为Excel,python-3.x,excel,pandas,csv,Python 3.x,Excel,Pandas,Csv,我正试图抓取一个网站并将其输出到Excel文件中。我设法创建了Excel文件,但列都乱了。请看图片。 如何将数据从CSV文件正确传输到Excel文件 我使用的代码是: import requests import pandas as pd from bs4 import BeautifulSoup page = requests.get('https://forecast.weather.gov/MapClick.php?lat=34.05349000000007&lon=-118.2
import requests
import pandas as pd
from bs4 import BeautifulSoup
page = requests.get('https://forecast.weather.gov/MapClick.php?lat=34.05349000000007&lon=-118.24531999999999#.XsTs9RMzZTZ')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.find(id = 'seven-day-forecast-body')
items = week.find_all(class_='tombstone-container')
period_names = [item.find(class_='period-name').get_text() for item in items]
short_descriptions = [item.find(class_='short-desc').get_text() for item in items]
temperatures = [item.find(class_='temp').get_text() for item in items]
weather_stuff = pd.DataFrame(
{
'period' : period_names,
'short_descriptions' : short_descriptions,
'temperatures' : temperatures,
})
print(weather_stuff)
weather_stuff.to_csv('weather.csv')
一个最简单的工作示例: df1=pd.DataFrame['a','b'],['c','d'], 索引=[“第1行”、“第2行”], 列=['col1','col2'] df1.to_exceloutput.xlsx 要指定图纸名称,请执行以下操作: df1.to_exceloutput.xlsx,sheet_name='sheet_name_1'
来源:您的问题是您创建的是csv文件,而不是excel文件。使用weather_stuff.to_excel'weather.xlsx'我认为你获得否决票的原因是你的问题不够清楚。您显示了两个屏幕截图,但其中哪一个是您的输出,另一个schreenshot告诉我们什么?您的问题是关于抓取正确的信息,还是关于在excel中导出信息?如果是后者,请参阅@datanoveler的建议。否则请澄清