How to get Output from this Program into Excel?

Click For Summary

Discussion Overview

The discussion revolves around how to export data from a Python program into an Excel file, specifically focusing on the process of converting a CSV file generated by the program into an Excel format. Participants explore various methods and tools related to this task, including the use of Excel's import features and Python libraries.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes their experience running a Python program that generates a CSV file and seeks guidance on how to convert it into an Excel file.
  • Several participants suggest using Excel's import function to load the CSV file, with some noting potential issues with Excel's handling of CSV files in different versions.
  • There is a discussion about whether Excel can recognize the CSV file based solely on its name and how to save it in different formats after importing.
  • Some participants propose that it might be possible to bypass the CSV format entirely and write directly to an Excel file using Python.
  • One participant mentions the use of the Pandas library for handling data in Python, suggesting it could be beneficial for the original poster's goals.
  • Another participant highlights the importance of ensuring that data in the CSV file is formatted correctly for Excel to interpret it properly.
  • There are inquiries about the original poster's broader goals, including streaming data from a website into Excel and cell phones.

Areas of Agreement / Disagreement

Participants express a range of views on the best methods for exporting data to Excel, with no clear consensus on a single approach. Some suggest using Excel's import features, while others propose using Python libraries directly. The discussion remains unresolved regarding the most efficient method to achieve the desired outcome.

Contextual Notes

Limitations include the original poster's uncertainty about the file creation process and the specifics of using Python to interact with Excel. There are also varying opinions on the effectiveness of Excel's import functionality across different versions.

Who May Find This Useful

This discussion may be useful for individuals learning Python programming, particularly those interested in data scraping and exporting data to Excel. It may also benefit users exploring different methods of data handling and integration between Python and Excel.

WWGD
Science Advisor
Homework Helper
Messages
7,804
Reaction score
13,107
Hi All,
I have been trying to teach myself programming recently (Python, mostly). I obviously still have a good way to go. User MarneMath was nice-enough to help me in this post a while back:
:
https://www.physicsforums.com/threads/classification-valuation-problem-baseball.893418/#post-5630986

on scraping data using Python. Now, I ran it in Anaconda and it did compile. I made a small change to it , changing "somefilename" to "somefilename.csv" . Now, this may be very simple, but, what do I need to do in order to get an Excel file with the .csv format? I know the program created a file, but I am not sure how to export it into an Excel file. Any ideas?
Thanks.
 
Technology news on Phys.org
It depends on which version of Excel.
Open a blank worksheet. Select 'Data' (from the ribbon), 'From Text', then select the desired CSV file.

There was something about Excel's CSV import in later versions that infuriated me, can't recall what, and in my opinion OpenOffice and LibreOffice do a better job of it.
 
  • Like
Likes   Reactions: WWGD
Asymptotic said:
It depends on which version of Excel.
Open a blank worksheet. Select 'Data' (from the ribbon), 'From Text', then select the desired CSV file.

There was something about Excel's CSV import in later versions that infuriated me, can't recall what, and in my opinion OpenOffice and LibreOffice do a better job of it.
Sorry, if this is dumb but how is Excel aware of the existence of the file, if the program , run in Anaconda, makes no mention of Excel? Will it recognize it by the name alone? EDIT: Never mind, thanks, Asymptotic.
 
WWGD said:
Sorry, if this is dumb but how is Excel aware of the existence of the file, if the program , run in Anaconda, makes no mention of Excel? Will it recognize it by the name alone?

Let's see if I'm thinking about this correctly.
You have created a CSV data file with the Python program, and want to load it into Excel.
In that case, run Excel, and import the CSV as described above. If you want to end up with an XLS formatted file, then save it in that format.

... or do you want the Python program create an XLS file directly?
 
  • Like
Likes   Reactions: WWGD
Asymptotic said:
Let's see if I'm thinking about this correctly.
You have created a CSV data file with the Python program, and want to load it into Excel.
In that case, run Excel, and import the CSV as described above. If you want to end up with an XLS formatted file, then save it in that format.

... or do you want the Python program create an XLS file directly?
The first is the case, thanks. Yes, I realized the import function would do it. Would Excel be able to do it directly without using Python, i.e., could I import an .html into Excel and save it as a .csv?
 
WWGD said:
The first is the case, thanks. Yes, I realized the import function would do it. Would Excel be able to do it directly without using Python, i.e., could I import an .html into Excel and save it as a .csv?

I don't think so, if you are using Python to scrape data from a web page then writing it into columns within the CSV file.
 
  • Like
Likes   Reactions: WWGD
Asymptotic said:
I don't think so, if you are using Python to scrape data from a web page then writing it into columns within the CSV file.
Excellent, thanks for everything.
 
@WWGD, I'm not sure from your posts if you realize that CSV stands for Comma Separated Values. As long as that is what you put in the file, Excel can natively read files with a .csv extension. Note that if the data in an individual cell has a comma in it, you need to surround that data with quotes. Excel will understand that as well.
 
  • Like
Likes   Reactions: harborsparrow and WWGD
WWGD said:
Hi All,
I have been trying to teach myself programming recently (Python, mostly). I obviously still have a good way to go. User MarneMath was nice-enough to help me in this post a while back:
:
https://www.physicsforums.com/threads/classification-valuation-problem-baseball.893418/#post-5630986

on scraping data using Python. Now, I ran it in Anaconda and it did compile. I made a small change to it , changing "somefilename" to "somefilename.csv" . Now, this may be very simple, but, what do I need to do in order to get an Excel file with the .csv format? I know the program created a file, but I am not sure how to export it into an Excel file. Any ideas?
Thanks.
I have directed Excel from a Visual Basic program. I have never tried it with Python - but here's a website that describes that:
http://pythonexcels.com/python-excel-mini-cookbook/
You should be able to bypass the csv file altogether - going directly to the *.xls or *.xlsx..
 
  • Like
Likes   Reactions: WWGD
  • #10
Excel allows the csv file to be imported as text through the wizard. In the wizard there are choices for how the data is delimitated. Select comma delimited and answer the questions as you click next within the wizard. Hope that helps
 
  • Like
Likes   Reactions: WWGD
  • #11
WWGD said:
Hi All,
I have been trying to teach myself programming recently (Python, mostly). I obviously still have a good way to go. User MarneMath was nice-enough to help me in this post a while back:
:
https://www.physicsforums.com/threads/classification-valuation-problem-baseball.893418/#post-5630986

on scraping data using Python. Now, I ran it in Anaconda and it did compile. I made a small change to it , changing "somefilename" to "somefilename.csv" . Now, this may be very simple, but, what do I need to do in order to get an Excel file with the .csv format? I know the program created a file, but I am not sure how to export it into an Excel file. Any ideas?
Thanks.
Click on the file to open it in Excel. In the menus select Save As...
and save the file. That's all.
 
  • #12
harborsparrow said:
Click on the file to open it in Excel. In the menus select Save As...
and save the file. That's all.
Thanks, but the file is not created " explicitly" that I can tell; what I did was just to use the import wizard.
 
  • #13
WWGD said:
Thanks, but the file is not created " explicitly" that I can tell; what I did was just to use the import wizard.

It seems I don't understand your question. Are you using office automation to animate Excel to do something?
 
  • #14
harborsparrow said:
It seems I don't understand your question. Are you using office automation to animate Excel to do something?
No sorry, I think I am a bit confused myself, as a beginner in this general area. I am using Python to scrape data from a site and then store it in Excel. I ultimately want to stream data continuously from the website into cell phones, but this was just a practice step, to get my hands wet. Sorry for the confusion.
 
  • #15
WWGD said:
No sorry, I think I am a bit confused myself, as a beginner in this general area. I am using Python to scrape data from a site and then store it in Excel. I ultimately want to stream data continuously from the website into cell phones, but this was just a practice step, to get my hands wet. Sorry for the confusion.

In general I'd suggest taking a look at Pandas dataframes module for Python, when you're ready. (Basically excel spreadsheets inside python.)

In the mean time you may want to check out "Automate the Boring Stuff", in particular, chapter 12 is about interfacing python and excel, and chapter 16 has stuff on automating text messages and emails, which is only slightly lower tech than what I think you're aiming for. (There's also chapters on reading and writing to files in general and web scraping and other stuff.)

In any case the book is made freely available by the author here:

https://automatetheboringstuff.com/

all Python and explicitly targets people new to programming
 
  • Like
Likes   Reactions: WWGD
  • #16
Is this question simply that you want to write the file to an excel?

df.to_excel("something.xlsx")
 
  • #17
MarneMath said:
Is this question simply that you want to write the file to an excel?

df.to_excel("something.xlsx")
Yes, in part, and also general ideas/methods on scraping data how-to's.
 

Similar threads

Replies
19
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 7 ·
Replies
7
Views
8K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
6K