AATU
AATU

Reputation: 87

CSV file to Excel

I'm trying to scrape a website and get the output in an Excel file. I manage to create the Excel file but the columns are all messed up (please see the pictures). How should I go about transferring the data correctly from the CSV file to the Excel file?

CSV file

Excel file

The code I used:

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')

Upvotes: 0

Views: 91

Answers (1)

oreopot
oreopot

Reputation: 3450

A minimilistic working example:

df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],
                   index=['row 1', 'row 2'],
                   columns=['col 1', 'col 2'])
df1.to_excel("output.xlsx") 

# To specify the sheet name:

df1.to_excel("output.xlsx", sheet_name='Sheet_name_1')

Source: Documentation

Upvotes: 1

Related Questions