【发布时间】:2019-04-12 00:09:59
【问题描述】:
我有一个 CSV 文件,我希望删除其中的前 11 行。该文件类似于:
"MacroTrends Data Download"
"GOOGL - Historical Price and Volume Data"
"Historical prices are adjusted for both splits and dividends"
"Disclaimer and Terms of Use: Historical stock data is provided 'as is' and solely for informational purposes, not for trading purposes or advice."
"MacroTrends LLC expressly disclaims the accuracy, adequacy, or completeness of any data and shall not be liable for any errors, omissions or other defects in, "
"delays or interruptions in such data, or for any actions taken in reliance thereon. Neither MacroTrends LLC nor any of our information providers will be liable"
"for any damages relating to your use of the data provided."
date,open,high,low,close,volume
2004-08-19,50.1598,52.1911,48.1286,50.3228,44659000
2004-08-20,50.6614,54.7089,50.4056,54.3227,22834300
2004-08-23,55.5515,56.9157,54.6938,54.8694,18256100
2004-08-24,55.7922,55.9728,51.9454,52.5974,15247300
2004-08-25,52.5422,54.1672,52.1008,53.1641,9188600
我只想要股票数据,而不想要其他任何数据。所以我希望删除前 11 行。此外,将有几个文本文件用于不同的代码。所以str_replace 似乎不是一个可行的选择。我用来获取 CSV 文件并将所需内容放入文本文件的函数是
function getCSVFile($url, $outputFile)
{
$content = file_get_contents($url);
$content = str_replace("date,open,high,low,close,volume", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);
}
我想要一个通用解决方案,它可以从 CSV 文件中删除前 11 行,并将剩余内容放入文本文件中。我该怎么做?
【问题讨论】:
-
file_put_contents(implode('', array_slice(file($url), 12))); -
@IslamElshobokshy 前 11 行中的字符串将根据文本文件而变化。所以我认为这不是那个问题的重复。
-
^ .. 但只需将其中一种方法扩展到 11。
标签: php csv file-get-contents php-7 file-handling