Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output on same line
#4
Several other issues with your code:
1. line #1 is not necessary. Later on you open the same file in append mode and if the file does not exist, it will be created when accessed for the first time.
2. You open the files, but never close the symbolfile. Anyway it's better to use with context manager, that will take care to close the file for you
3. no need to open the file in the loop and close it with every iteration, better open it outside of the loop, it's more efficient.
4. It's recommended to use format method for string formatting, or even the new f-strings (that's only python 3.6)
5. Although it's perfectly OK to read the entire symbolfile into memory, it's better to iterate over it line by line.


from yahoo_finance import Share
with open('symbols.txt') as symbolfile, open('outputyahoo.txt', 'a') as savefile:
   for symbol in symbolfile:
       symbol = symbol.strip()
       stocks = Share(symbol)
       price = stocks.get_price()
       my_output = '{}: {}\n'.format(symbol, price)
       print(my_otput, end = '')
       savefile.write(my_otput)
Reply


Messages In This Thread
Output on same line - by takaa - May-03-2017, 10:02 AM
RE: Output on same line - by metulburr - May-03-2017, 10:10 AM
RE: Output on same line - by takaa - May-03-2017, 11:24 AM
RE: Output on same line - by buran - May-03-2017, 11:48 AM
RE: Output on same line - by takaa - May-03-2017, 03:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 6,860 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  How to do next line output from CSV column? atomxkai 2 2,827 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
  How to input & output parameters from command line argument shantanu97 1 3,766 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Jupiter Notebook does not show output line Renym 3 3,562 Apr-26-2020, 11:21 AM
Last Post: jefsummers
  A question about subprocess taking input from command line and returning output! Aurimas 8 6,726 May-15-2019, 04:02 PM
Last Post: Aurimas
  String output displaying \n instead of new line... nicklesprout 4 28,369 Jul-28-2017, 08:01 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020