创建.csv文件表Python

时间:2018-02-06 04:16:49

标签: python csv datatable beautifulsoup

我正在尝试创建.csv文件表。我正在努力将数据组织成表格格式。如何在正确的标题位置下组织输出数据?

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

4 个答案:

答案 0 :(得分:1)

首先,您可能需要import csv,这可以大大减少您输出csv信息的问题。

文档链接位于:https://docs.python.org/2/library/csv.html

另外我想指出你还没有真正解释过csv表的结构或目的,所以我无法真正帮助你

答案 1 :(得分:1)

是的,你绝对应该使用csv模块,它会简化你的代码。在这种情况下,看起来csv文件中应该只有两行 - 标题和数据行。

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'})))

答案 2 :(得分:0)

你也可以使用熊猫。

例如:

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']])

答案 3 :(得分:0)

您可以使用csv模块。 这是一个简单的例子,

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文件如下所示:

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