自动化数据清理并将表格保存为 CSV 的过程

问题描述 投票:0回答:1

我只是想按照与下面定义的完全相同的方式清理“filePaths”变量中的 2 个 csv 文件并将列添加到其中(您可以忽略该过程),然后将这些数据保存到新的 csv 文件中。

这里的两个问题主要是“如何将一堆文件路径分组到一个名为“filepaths”的变量中 以及“如何使用 KDB/Q 中的“each”函数?来循环执行该过程”

提前致谢!

// Define the list of file paths
filePaths: (`"$/Users/yuanhanlim/Desktop/KDBQ/Final_Project/MSFT_orderbook.csv",
        `"/Users/yuanhanlim/Desktop/KDBQ/Final_Project/AAPL_orderbook.csv";)

// Loop through each file path
{
    filePath;
    formatString: "IIIIIIIIIIIIIIIIIIII";
    // Read data from CSV
    data_QTE: (formatString; enlist ",") 0: filePath;

    // Calculate MidPrice
    data_QTE: update MidPrice: (Bid_Price1 + Ask_Price1) % 2 from data_QTE;

    // Compute total bid volume
    data_QTE: update Nbt: sum (Bid_Vol1; Bid_Vol2; Bid_Vol3; Bid_Vol4; Bid_Vol5) from data_QTE;

    // Compute total ask volume
    .
    . 
    // Remove the first row
    new_qte: delete from new_qte where i = 0;

    // Save cleaned data to CSV
    savePath: `$"/Users/yuanhanlim/Desktop/KDBQ/Final_Project/cleaned_" , last filePath;
    : savePath 0: csv 0: new_qte;
  } each filePaths;


First error I got: 
valuation error:



[0]  filePaths: (`"/Users/yuanhanlim/Desktop/KDBQ/Final_Project/MSFT_orderbook.csv",
          `"/Users/yuanhanlim/Desktop/KDBQ/Final_Project/AAPL_orderbook.csv";)
          ^
2nd error I got; 
evaluation error:

type

[2]  
   // Save cleaned data to CSV
   savePath: `$"/Users/yuanhanlim/Desktop/KDBQ/Final_Project/cleaned_" , last filePath;
             ^
   : savePath 0: csv 0: new_qte;

[1]  (.q.each)


[0]  
  : savePath 0: csv 0: new_qte;
} each filePaths;
  ^
kdb q
1个回答
0
投票

文件的路径需要是文件描述符,https://code.kx.com/q/ref/file-text/#load-csv不是符号。将其更改为

$":path" or hsym 
$"path" https://code.kx.com/q/ref/hsym/

© www.soinside.com 2019 - 2024. All rights reserved.