Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output on same line
#1
Hi,

I made a little code to extract some stock quotes. 

open('outputyahoo.txt', 'w').close()
from yahoo_finance import Share
symbolfile = open('symbols.txt')
 
symbolslist = symbolfile.readlines()
 
for symbol in symbolslist:
    stocks = Share(symbol)
    price = stocks.get_price()
    print(symbol +" " + price)
 
    saveFile = open('outputyahoo.txt', 'a')
    saveFile.write((symbol +" " + price)+ '\n')
    saveFile.close()
The output comes like 

Output:
ASFI  7.15 3828.HK  1.19 564.SI  0.88
I would like to output to come as
ASFI 7.15
3828.HK 1.19
etc

Thanks for the help
Reply
#2
most likely it is symbol as it is the whole line of the file which would include the newline at the end
>>> '\nblah\n'.strip()
'blah'
So I would try

symbol.strip()
Recommended Tutorials:
Reply
#3
Tnx

I solved it with this line 

symbolslist = symbolfile.read()

symbolslist = symbolslist.split('\n')
Reply
#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
#5
(May-03-2017, 11:48 AM)buran Wrote: Several other issues with your code:

Tnx for pointing out. Makes the learning curve keep going up and to the right
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and storing a line of output from pexpect child eagerissac 1 4,221 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  How to do next line output from CSV column? atomxkai 2 2,079 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
  How to input & output parameters from command line argument shantanu97 1 2,552 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Jupiter Notebook does not show output line Renym 3 2,638 Apr-26-2020, 11:21 AM
Last Post: jefsummers
  A question about subprocess taking input from command line and returning output! Aurimas 8 5,162 May-15-2019, 04:02 PM
Last Post: Aurimas
  String output displaying \n instead of new line... nicklesprout 4 25,814 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