title: Create .csv file table Python
body: <p>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? </p>
  
  <pre><code>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'))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/pbEqR.png" rel="nofollow noreferrer">what my csv file looks like</a></p>