A
A
Ander8132018-12-30 21:20:45
Python
Ander813, 2018-12-30 21:20:45

How to use python to save data in csv format in different cells when opened in excel?

I wrote the simplest parser, but I can’t save it in csv format so that in Excel the data is saved in different cells (A-day of the week, B-number, C-temperature).

import requests
from bs4 import BeautifulSoup
import lxml
import csv

url = "https://world-weather.ru/pogoda/russia/saint_petersburg"
temperatures=[]
def get_html(url):
    r = requests.get(url)
    return r.text

def get_temperature(html):
    a = 0
    global temperatures
    soup = BeautifulSoup(html, 'lxml')
    temperature_get = soup.find('ul', id="vertical_tabs")
    for temperature in temperature_get.find_all('div', class_='day-temperature'):
        temperatures.append({
            'week_day' : temperature_get.find_all('div', class_='day-week')[a].text,
            'day_number' : temperature_get.find_all('div', class_='numbers-month')[a].text,
            'temperature': temperature_get.find_all('div', class_='day-temperature')[a].text,
        })
        a += 1
    return temperatures
def save(temp,path):
    global temperatures
    with open(path, 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=' ')
        writer.writerow(('День недели', 'Число', 'Температура'))

        for temperature in temperatures:
            writer.writerow((temperature['week_day'], temperature['day_number'], temperature['temperature']))


def main():
    print(get_temperature(get_html(url)))
    save(get_temperature, 'temperature.csv')
if __name__ == "__main__":
    main()

5c290c0dee08d156543638.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan, 2019-12-31
@Ander813

the cells are merged because if you open it directly in Excel (double click on the file and the default Excel program for .csv), it does not ask what separator to use, but shoves everything as it suits him.
so:
- either google " How to import CSV to Excel " (Tab "data" ->> "from text"), and there will be an EXPLICIT choice of delimiter, select a space in your case - and everything will be OK.
- either use Libre Office, which, when opened, is interested in "what kind of separator we will use" - select a space, and it will be OK.
In addition:
- delimiter=' 'here in your case the separator is indicated
- "Day of the week" - better "Day_of the week" - well, still separate the space, you never know.
- Well, of course, you don’t need Pandas, for this task, it’s too much.

X
x67, 2018-12-30
@x67

Use the pandas library:
import pandas as pd
df=pd.DataFrame()
df['day']=days_list
df['temp']=temp_list
df.to_csv('/home/ander/res.csv'
) from lists, but from anything. You can add data line by line, but it's a little slower. Panda has some work specifics that you need to get used to, but this is the coolest tool for working with csv.
I will format the code later from the computer

M
MechanicZelenyy, 2019-12-31
@MechanicZelenyy

When opening a csv file in excel, you need to specify the delimiter you used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question