Taylor Rhodes
Taylor Rhodes

Reputation: 33

Create .csv file table Python

I am trying to create a .csv file table. I am struggling to organize the data into table format. How do I organize output data under their proper header position?

import urllib
import urllib.request
from bs4 import BeautifulSoup
import os
from string import ascii_uppercase

def make_soup(url):
    thepage = urllib.request.urlopen(url)
    soupdata = BeautifulSoup(thepage, "html.parser")
    return soupdata

stockdatasaved=""
soup = make_soup("https://finviz.com/quote.ashx?t=mbot")
for record in soup.findAll('td', {"class":"snapshot-td2"}):
        stockdata=""
        stockdata=stockdata+','+record.text
        stockdatasaved = stockdatasaved +"\n" +stockdata[1:]

header="Index,MarketCap,Income,Sales,Bk/sh,$/sh,Div,Div%,Empl,Optionable,Shortable,Recom,P/e,Forward P/e,PEG," \
       "P/s,P/b,P/c,P/fcf,QuickRatio,CurrentRatio,Debt/Eq,LT Debt/Eq,SMA20,Eps,Eps next Y,Eps next Q,Eps this Y," \
       "Eps next 5Y,Eps past 5Y,Sales past 5Y,Sales Q/Q, Earnings, Sma50,Insider Own,Insider Trans,Inst Own," \
       "Inst trans,ROA,ROE,ROI,Gross Margin,Oper. Margin,Profit Marg,Payout,SMA200,Shs Outstand,Shs Float," \
       "Short Float,Short Ratio,Target Price,52W Range,52W High,52W Low,RSI(14),Rel Volume,Avg Volume,Volume," \
       "Perf Week,Perf Month,Perf Q,Perf Half Y,Perf Y,Perf YTD,Beta,ATR,Volatility,Prev Close,Price,Change"+"\n"
file = open(os.path.expanduser("Stocks.csv"),"wb")
file.write(bytes(header, encoding="ascii",errors='ignore'))
file.write(bytes(stockdatasaved,encoding="ascii",errors='ignore'))

what my csv file looks like

Upvotes: 0

Views: 5570

Answers (4)

Brian
Brian

Reputation: 1898

Yes, you should definitely use the csv module, it will simplify your code. In this case, it looks like there should be only two rows in the csv file - the headers and the data row.

import csv
import urllib.request
from bs4 import BeautifulSoup

soup = BeautifulSoup(
    urllib.request.urlopen('https://finviz.com/quote.ashx?t=mbot'),
    'html.parser'
)

with open('output.csv', 'wt') as file:
    writer = csv.writer(file)

    # write header row
    writer.writerow(map(lambda e : e.text, soup.find_all('td', {'class':'snapshot-td2-cp'})))

    # write body row
    writer.writerow(map(lambda e : e.text, soup.find_all('td', {'class':'snapshot-td2'})))

Upvotes: 1

aprasanth
aprasanth

Reputation: 1099

You can use the csv module. Here is a simple example for this,

import csv

# create a csv file object and open
with open('/path/sample.csv', 'wt') as csvfile:
    writer = csv.writer(csvfile, delimiter='\t', lineterminator='\n', )
    # Add the header row
    writer.writerow(['Odd', 'Even'])
    for i in range(1,20,2):
        # Add the data row
        writer.writerow([i, i+1])

sample.csv file look like this:

+-----+-------+
| Odd |  Even |
+-----+-------+
|   1 |     2 |
|   3 |     4 |
|   5 |     6 |
|   7 |     8 |
|   9 |    10 |
|  11 |    12 |
|  13 |    14 |
|  15 |    16 |
|  17 |    18 |
|  19 |    20 |
+-----+-------+

Upvotes: 0

Harsha Biyani
Harsha Biyani

Reputation: 7268

You also can use Pandas.

Ex:

import pandas as pd
def write_to_csv(self,output):
  df_output =  pd.DataFrame(output,columns=['Index','Name'],dtype=float)
        df_output.to_csv('output.csv')

write_to_csv([['1','IBM'],['2','Cogni'],['3','Toyota'],['4','tomtom']])

Upvotes: 0

Max Collier
Max Collier

Reputation: 691

For a start you may want to import csv, this should greatly reduce your issues with outputting csv information.

The link to the documentation is at: https://docs.python.org/2/library/csv.html

Also I would like to like to point out that you haven't really explained the structure or purpose of your csv table so I can't really help you with that

Upvotes: 1

Related Questions