【问题标题】:Getting data from list of text files following some pattern按照某种模式从文本文件列表中获取数据
【发布时间】:2015-05-13 10:17:57
【问题描述】:

我想这并不难,但我真的被困住了。 我有一些文件,用一串 9 个数字命名,例如:

003485295.lst
005847283.lst
092348235.lst
...

每个文件都标识一个测量站,并包含一个包含两列的表格,datevalue,由空格字符分隔。例如,003485295.lst 是:

date value //header row here!
2014-01-03-07:00-00 2.2
2014-01-04-07:00-00 3.1
2014-01-05-07:00-00 28.6
2014-01-06-07:00-00 2.5
2014-01-14-07:00-00 5.6
...

有些日期在所有文件(电台)中是通用的,但有些不是。

我在寻找什么

我选择了一个特定的日期,比如2014-01-06。我想:

  • 创建一个空的结果文件,比如2014-01-06.txt
  • 循环浏览我的所有*********.lst 文件;
  • 搜索站********当天是否进行了一些测量MM.M
  • 在我的结果文件中添加一行,遵循模式 stationId-value,例如 ******** MM.M(也可以是 M.MMMM.M)。

因此,所需的输出 2014-01-06.txt 类似于:

003485295 2.5 //as we read in 003485295.lst
001022903 6.4
001022905 6.6
001022907 10.3
001026202 30.6
...

应排除在当天没有任何价值的电台。我在 Windows 上,手上有 RMATLAB,但也可以使用其他工具。

【问题讨论】:

  • 你的 lst 文件的体积是多少,每个文件有多少数据(只是为了建议一种可扩展的方法)?
  • 现在不是很大,大约。 100 个 lst 文件,每个文件不超过 200 行。

标签: regex r matlab


【解决方案1】:

使用 R 的方法

我会通过以下方式解决这个问题:

  1. 使用 read.csv 函数将所有文件上传到 R 中的单个数据表(您的文件基本上是用空格分隔的 CSV,所以这应该相对容易)

  2. 按日期对值进行排序

  3. 用因子替换日期

  4. 按日期因子(如this)拆分data.table。

  5. 通过 data.tables 执行 for 并使用 write.csv 函数将每个单独转储为 CSV

使用 Excel 的方法

  1. 使用 DIR 函数(循环文件)并读取每个文件 - 链接 here,通过 VBA 上传所有文件。对空格使用 Split 函数来标记字符串。将所有数据转储到一个工作表中

  2. 手动按日期列排序数据

  3. 编写一个简短的 VBA 宏,使用 this 和如下代码将数据转储到单独的文件中:

    将文件日期调暗为字符串 直到 currDate.Value = "" 如果 fileDate currDate 那么 '创建新文件日期 文件日期 = 当前日期 万一 '将数据转储到文件中 写#fileNo, Cells(currDate.Row, 2) 循环

在整个练习中,基本上记住文件是space delimited CSVs,这将有助于获得正确的方法来读取和写入文件。

【讨论】:

  • 方法有点不同,但我做到了。我只想指出write.csv 不行,因为它不允许你使用append=TRUE。改用write.table
【解决方案2】:

这里有一个 Matlab 函数可以完成这个任务。 (假设您至少有 Matlab R2014b,因为它使用 Matlab 日期时间。如果没有,您可以改用 datenum)。

function result = getDateData(dateToLookup)
    % Get a list of all *.lst files
    d = dir('*.lst');

    result = [];
    for i=1:length(d)
        % Read in the data file using readtable.  Delimiter is space, but need
        % to skip the first line because it doesn't follow this pattern
        t = readtable(d(i).name, 'FileType', 'text', 'Delimiter', ' ', ...
            'HeaderLines', 1, 'ReadVariableNames', false);
        t.Properties.VariableNames = {'Date', 'Data'};

        % Convert the first column to datetime
        t.Date = datetime(t.Date, 'Format', 'yyyy-MM-dd-HH:mm-ss');

        % Looks like you want to lookup by date (and not consider
        % hours/minutes/seconds, so shift these to start of day)
        t.Date = dateshift(t.Date, 'start', 'day');

        % Add in the filename to the table (minus the .lst)
        t.FileName = repmat({strrep(d(i).name, '.lst', '')}, height(t), 1);

        % Append table to the result
        result = [result; t]; %#ok<AGROW>
    end

    % This is the date you're asking for
    dt = datetime(dateToLookup);

    % Find all of the matches
    matches = result(result.Date == dt, :);

    % Create a new table as the result and write it out
    result = table(matches.FileName, matches.Data, ...
        'VariableNames', {'FileName', 'Data'});
    writetable(result, 'data.txt', 'Delimiter', ' ');
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    相关资源
    最近更新 更多