How to use Python to save data in csv format in different cells when opened in Excel?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
13 views
Written with a simple parser, but I cannot save it in csv format so that Excel data is stored in different cells(A-day week, B-number, C-the 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(('Day of week', 'Number', 'Temperature')) 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
by | 13 views

3 Answers

0 like 0 dislike
the cells are merged because, if you open in Excel directly (double click on the file and Excel the default program for .csv), it asks what delimiter to use, and pushes everything as it is convenient.
therefore:
- or Google "How to import CSV to Excel" (Tab "data" ->>"from text") and there will be a CLEAR choice of the delimiter, choose a space in Your case-and everything will be OK.
- either use Libre Office ,which when opened , asked "what is the delimiter we will use the" - choose a space and you will be OK.

Besides:
- delimiter=' ' here in Your case, you specify the separator
- "Day of the week" - better: "Weekday" - well, still space to share, you never know.
- well, Pandas are certainly not necessary, for this task, too.
by
0 like 0 dislike
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')
You can import from lists, and anything. You can line by line and add data, but it is a bit slower. The Panda has some specifics, which need to get used to, but this is the coolest tool to work with csv.
Code and later with the company format
by
0 like 0 dislike
When you open the csv file in excel you need to specify the used separator.
by

Related questions

0 like 0 dislike
3 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked May 21, 2019 by CreativeStory
110,608 questions
257,186 answers
0 comments
36,635 users