How to get Output from this Program into Excel?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
16 replies · 2K views
Messages
7,819
Reaction score
13,151
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.
 
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
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
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.
 
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.
 
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?
 
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.
 
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
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.