在`设置数据文件分隔符“|||”`不起作用多字符分隔符

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

我有一个三重管作为分隔符输入文件example.data,日期在第一列中,也有一些或多或少不可预知的文字在最后一栏:

2019-02-01|||123|||345|||567|||Some unpredictable textual data with pipes|,
2019-02-02|||234|||345|||456|||weird symbols @ and commas, and so on.
2019-02-03|||345|||234|||123|||text text text

当我尝试运行以下gnuplot5脚本

set terminal png size 400,300
set output 'myplot.png'

set datafile separator "|||"
set xdata time
set timefmt "%Y-%m-%d"
set format x "%y-%m-%d"
plot "example.data" using 1:2 with linespoints

我得到以下错误:

line 8: warning: Skipping data file with no valid points

plot "example.data" using 1:2 with linespoints
                                              ^
"time.gnuplot", line 8: x range is invalid

更奇怪的是,如果我改变的最后一行

plot "example.data" using 1:4 with linespoints

那么它的工作原理。这也适用于1:71:10,但不支持其他的数字。为什么?

command-line gnuplot
2个回答
1
投票

当使用

set datafile separator "chars"

语法,该字符串不被视为一个长隔板。相反,引号之间列出的每个字符变成自身的分隔符。从[Janert,2016]:

如果你提供了一个明确的字符串,然后在字符串中的每个字符将被视为一个分隔符。

因此,

set datafile separator "|||"

其实就相当于

set datafile separator "|"

和线

2019-02-05|||123|||456|||789

因为如果它有十列,其中仅列1,4,7,10非空对待。


解决方法

找一些其它的字符是不太可能出现的数据集(在下文中,我会假设\t为例)。如果你不能转储具有不同的分隔符的数据集,使用sed通过|||更换\t

sed 's/|||/\t/g' example.data > modified.data # in the command line

然后继续

set datafile separator "\t"

modified.data作为输入。


1
投票

基本上,你自己给出了答案。

  1. 如果你可以在你的数据影响了分离器,使用通常不会在您的数据或文字出现一个分隔符。我一直以为\t为那个制造。
  2. 如果你不能在你的数据影响了分离器,使用外部工具(AWK,Python和Perl中,...)来修改数据。在这些语言中,它可能是一个“一班车”。 gnuplot的没有直接的替换功能。
  3. 如果你不想安装外部工具,并希望确保平台的独立性,还有一种方式与gnuplot的做到这一点。不只是“一个班轮”,但几乎没有什么你不能也做gnuplot的;-)。

编辑:简化版本与从@Ethan(https://stackoverflow.com/a/54541790/7295599)的输入端。

假设你有一个名为$Data数据集的数据。下面的代码将与|||取代\t,并将结果为$DataOutput

### Replace string in dataset
reset session

$Data <<EOD
# data with special string separators
2019-02-01|||123|||345|||567|||Some unpredictable textual data with pipes|,
2019-02-02|||234|||345|||456|||weird symbols @ and commas, and so on.
2019-02-03|||345|||234|||123|||text text text
EOD

# replace string function
# prefix RS_ to avoid variable name conflicts
replaceStr(s,s1,s2) = (RS_s='', RS_n=1, (sum[RS_i=1:strlen(s)] \
    ((s[RS_n:RS_n+strlen(s1)-1] eq s1 ? (RS_s=RS_s.s2, RS_n=RS_n+strlen(s1)) : \
    (RS_s=RS_s.s[RS_n:RS_n], RS_n=RS_n+1)), 0)), RS_s)

set print $DataOutput
do for [RS_j=1:|$Data|] {
    print replaceStr($Data[RS_j],"|||","\t")
}
set print

print $DataOutput
### end of code

输出:

# data with special string separators
2019-02-01  123 345 567 Some unpredictable textual data with pipes|,
2019-02-02  234 345 456 weird symbols @ and commas, and so on.
2019-02-03  345 234 123 text text text
© www.soinside.com 2019 - 2024. All rights reserved.