創建CSV標題行變量我已經做了一些similar in Bash但不知道如何在Ruby中的2-3簡練的線條做到這一點,但現在看來,這可以用正確的編碼功夫瞬間完成-FU。如何在Ruby中
我有 「FILE.CSV」 有標題行,看起來像:
Ticker,"Price","Market Cap","Average Volume","Analyst Recom","Relative Strength Index (14)","Sector","Industry","Dividend Yield","Beta","52-Week Low","52-Week High","50-Day Low","50-Day High","Company","50-Day Simple Moving Average","Country","P/E","Forward P/E","PEG","P/S","P/B","P/Cash","P/Free Cash Flow","Payout Ratio","EPS (ttm)","EPS growth this year","EPS growth next year","EPS growth past 5 years","EPS growth next 5 years","Sales growth past 5 years","EPS growth quarter over quarter","Sales growth quarter over quarter","Shares Outstanding","Shares Float","Insider Ownership","Insider Transactions","Institutional Ownership","Institutional Transactions","Float Short","Short Ratio","Return on Assets","Return on Equity","Return on Investment","Current Ratio","Quick Ratio","LT Debt/Equity","Total Debt/Equity","Gross Margin","Operating Margin","Profit Margin","Performance (Week)","Performance (Month)","Performance (Quarter)","Performance (Half Year)","Performance (Year)","Performance (Year)","Average True Range","Volatility (Week)","Volatility (Month)","20-Day Simple Moving Average","200-Day Simple Moving Average","Change from Open","Gap","Relative Volume","Change","Volume","Earnings Date","No."
,隨後爲約7000行看起來像:
FCD,27.89,,0.94,,66.75,"Financial","Exchange Traded Fund",3.13%,,19.75%,-0.36%,6.37%,-0.36%,"Focus Morningstar Consumer Defensive ETF",2.28%,"USA",,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,0.36%,3.07%,9.93%,10.85%,,2.01%,0.12,0.04%,0.21%,1.26%,6.69%,0.00%,-0.04%,0.96,-0.04%,900,,2186
FCE-A,14.59,2496.64,960.33,2.50,54.76,"Financial","Property Management",,2.83,56.55%,-24.87%,36.61%,-7.77%,"Forest City Enterprises Inc.",11.49%,"USA",,69.48,,2.2
5,1.58,10.87,,,-0.02,410.77%,250.00%,-10.06%,8.00%,1.54%,-28.77%,-9.00%,171.12,136.94,0.26%,-8.25%,74.80%,-0.13%,4.62%,6.59,0.46%,-0.12%,0.54%,,,4.35,4.35,39.54%,
4.82%,4.60%,-4.01%,8.96%,25.45%,13.10%,-22.80%,23.43%,0.44,3.07%,2.98%,-0.89%,1.49%,-1.62%,0.00%,0.47,-1.62%,449874,12/8/2010 4:30:00 PM,2187
給定一個股票代碼爲 「FCD」我正在嘗試將大約30個從頭字段中取出的新變量集中分配給匹配「FCD」行的值。
每個新變量都以fv_
作爲前綴,並且在字段名稱中減去所有標點符號,空格,引號等(變量不友好的東西)。
所以對於「FCD」我想給我的腳本:
fv_Ticker="FCD"
fv_Price=27.89
fv_MarketCap=""
fv_VolatilityMonth=0.21 # if get String not Float because of trailing % in "0.21%" that's okay, will deal with it later
etc.
我要指出,我退出使用任何類型的CSV.read
或CSV.foreach
由於horrible slowdown using native Ruby 1.9.x CSV objects,歷時分鐘閱讀,因此是不可接受的在重複運行的實時應用程序中。
相反,我一直在使用一個Ruby管「的awk」指定從文件瞬間這樣讀取各個變量:
$stock="FCD"
$dividend_yield = IO.readlines("|awk -F, '$1==\"#{$stock}\" {print $9}' finviz.AllStocks.csv")[0].to_f
$beta = IO.readlines("|awk -F, '$1==\"#{$stock}\" {print $10}' AllStocks.csv")[0].to_f
,但現在它變得太毛茸茸到不能一概而論。它需要處理任何帶有未知字段的CSV文件,直到它看到的第一行。
爲什麼變量?爲什麼不是'{「FCD」=> someObject}'的散列? – Linuxios 2012-02-29 16:09:20
散列也可以,如果不是更好,但它需要很快。 [先前](http://stackoverflow.com/q/8476769/1069375)將CSV讀入Ruby失敗了。 – Marcos 2012-02-29 16:23:12
用變量填充符號表不能比散列效率低。而且,對任何事情使用全局變量,尤其是這個,不是一個好主意。 – Linuxios 2012-02-29 16:30:10