Unknown characters fetching stock data from yahoo in csv format
Question: When I try to get historical stock data from yahoo finance in python I'm
getting correct output in html, but in csv, it returns unknown characters

output of csv :

    
    
    摁彪汣獯ⱥ汃獯ⱥ慄整䠬杩ⱨ潌ⱷ灏湥匬浹潢ⱬ潖畬敭㈊㄰ⴴ㐰㈭ⰵ䡙住ㄬ㐹㄰〶ⰰ㐳㐮ⰸ㔳〮㤹㤹ⰸ㐳㈮〹〰ⰱ㐳㐮ⰸ㔳〮㤲㤹ⰹ
    

Here is my code :

    
    
     def run():
       yahoo=Share('YHOO')
       file1=open("scripts/hisdata.csv","w")
       file1.write("Adj_close,Close,Date,High,Low,Open,Symbol,Volume\n")
       h= yahoo.get_historical('2014-04-25', '2014-04-29')
       h=h[0]
       a=h.values()
       a=','.join(a)
       a=a.split(',')
       vol=a[0]
       sym=a[1]
       adjcls=a[2]
       high=a[3]
       low = a[4]
       date = a[5]
       close = a[6]
       opn = a[7]
       file1.write(date+",")
       file1.write(sym+",")
       file1.write(vol+",")
       file1.write(adjcls+",")
       file1.write(high+",")
       file1.write(low+",")
       file1.write(close+",")
       file1.write(opn+",")
       print vol
       print date
    
       return (vol,adjcls,high,low,close,opn)
    


Answer: You could try the following approach which makes use of Python's
[`csv`](https://docs.python.org/2/library/csv.html#module-csv) module to
automatically create the correct format for your output:

    
    
    from yahoo_finance import Share
    import csv
    
    def run():
        yahoo = Share('YHOO')
    
        with open("scripts/hisdata.csv", 'wb') as file1:
            csv_output = csv.writer(file1)
    
            # First write the header row
            heading = ['Adj_Close', 'Close', 'Date', 'High', 'Low', 'Open', 'Symbol', 'Volume']
            csv_output.writerow(heading)
    
            h = yahoo.get_historical('2014-04-25', '2014-04-29')[0]
            csv_output.writerow([h[value] for value in heading])
    
            print h['Volume']
            print h['Date']
    
            return ([h[value] for value in ['Volume', 'Adj_Close', 'High', 'Low', 'Open']])
    
    print run()
    

The `yahoo_finance` module returns each entry as a dictionary, so it is easier
and safer to extract each entry by name rather than trying to list the values.

For me this creates an `output.csv` file containing the following:

    
    
    Adj_Close,Close,Date,High,Low,Open,Symbol,Volume
    35.830002,35.830002,2014-04-29,35.889999,34.119999,34.369999,YHOO,28736000
    

And displays the following output:

    
    
    28736000
    2014-04-29
    ['28736000', '35.830002', '35.889999', '34.119999', '34.369999']
    

The data you are seeing is probably due to the method you are using to view
the file, it appears to be decoding it incorrectly. The file should be opened
as bytes e.g. ASCII. You should try loading it into a spreadsheet application,
e.g. Excel.

For LibreOffice Calc try the following:

[![LibraOffice import
dialog](http://i.stack.imgur.com/C49Eg.png)](http://i.stack.imgur.com/C49Eg.png)

To write all of the returned row data to the csv file, the following script
could be used:

    
    
    from yahoo_finance import Share
    import csv
    
    def run():
        yahoo = Share('YHOO')
    
        with open('scripts/hisdata.csv', 'wb') as file1:
            csv_output = csv.writer(file1)
            heading = ['Adj_Close', 'Close', 'Date', 'High', 'Low', 'Open', 'Symbol', 'Volume']
            csv_output.writerow(heading)
    
            for row in yahoo.get_historical('2014-04-25', '2014-04-29'):
                csv_output.writerow([row[value] for value in heading])
    
    run()
    

