PHP-解析txt文件

发布于 2024-12-07 19:49:37 字数 1424 浏览 0 评论 0原文

我想解析这个文件:

Case    Given       Predicted
No      Class       Class
1       ?               0               [0.80]
2       ?               0               [0.80]
3       ?               0               [0.80]
4       ?               1               [0.75]
5       ?               0               [0.80]
6       ?               0               [0.80]
7       ?               1               [0.75]
8       ?               0               [0.80]
9       ?               0               [0.80]
10      ?               0               [0.80]
11      ?               1               [0.75]
12      ?               0               [0.80]
13      ?               0               [0.80]
14      ?               0               [0.80]
15      ?               0               [0.80]
16      ?               0               [0.80]
17      ?               0               [0.80]
18      ?               0               [0.80]
19      ?               0               [0.80]
20      ?               0               [0.80]

特别是,我想从第三列(“预测类”)中获取值。我打开文件感谢:

$txt_file    = file_get_contents('simone.result');
$array        = explode("\n", $txt_file);
array_shift($array);
array_shift($array);
array_shift($array);

我有这个:

Array ( [0] => 1 ? 0 [0.80] [1] => 2 ? 0 [0.80] 

好的,这是正确的。但我只想要这个数组的每个键中的第三个值(“0”或“1”)。有人可以帮助我吗?

太感谢了!

i'd like to parse this file:

Case    Given       Predicted
No      Class       Class
1       ?               0               [0.80]
2       ?               0               [0.80]
3       ?               0               [0.80]
4       ?               1               [0.75]
5       ?               0               [0.80]
6       ?               0               [0.80]
7       ?               1               [0.75]
8       ?               0               [0.80]
9       ?               0               [0.80]
10      ?               0               [0.80]
11      ?               1               [0.75]
12      ?               0               [0.80]
13      ?               0               [0.80]
14      ?               0               [0.80]
15      ?               0               [0.80]
16      ?               0               [0.80]
17      ?               0               [0.80]
18      ?               0               [0.80]
19      ?               0               [0.80]
20      ?               0               [0.80]

Especially, i want to take values from third column ("Predicted class").I open file thanks to:

$txt_file    = file_get_contents('simone.result');
$array        = explode("\n", $txt_file);
array_shift($array);
array_shift($array);
array_shift($array);

And I have this:

Array ( [0] => 1 ? 0 [0.80] [1] => 2 ? 0 [0.80] 

Ok this is correct. But I want only the third value ("0" or "1") in each key of this array. Does anyone can help me please?

Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千仐 2024-12-14 19:49:38
$lines = file('simone.result');
$column3 = array();
for( $i = 2, $ilen = count( $lines ); $i < $ilen; $i++ )
{
    $columns = preg_split( '/\s+/', $lines[ $i ] );
    $column3[] = $columns[ 2 ];
}

var_dump( $column3 );
$lines = file('simone.result');
$column3 = array();
for( $i = 2, $ilen = count( $lines ); $i < $ilen; $i++ )
{
    $columns = preg_split( '/\s+/', $lines[ $i ] );
    $column3[] = $columns[ 2 ];
}

var_dump( $column3 );
萌辣 2024-12-14 19:49:38

为什么不简单地将字符串分解为一个空格并使用您想要的值呢?

Why not just simply explode your string by a space and use the value you want?

苏别ゝ 2024-12-14 19:49:38

像这样的东西吗?

foreach( $array as $key => $value ) {
    $split = explode( " ", $value );
    $array[$key] = $split[2]; 
}

Something like this?

foreach( $array as $key => $value ) {
    $split = explode( " ", $value );
    $array[$key] = $split[2]; 
}
感情旳空白 2024-12-14 19:49:38

检索文件的所有行

$lines = file($txt_file);

,然后循环它们:

foreach($lines as $line ) 
{
    list($v1,$v2,$v3,$v4) = explode("\n", trim($line)); //assuming separator is a space \t for tab

    //take the values you need here
    $tmp[] = $v3;

}

print_r($tmp);

如果文件写得好,这也可以工作:)

retrieve all the lines of the file

$lines = file($txt_file);

then loop on them:

foreach($lines as $line ) 
{
    list($v1,$v2,$v3,$v4) = explode("\n", trim($line)); //assuming separator is a space \t for tab

    //take the values you need here
    $tmp[] = $v3;

}

print_r($tmp);

as well this work if the file is well written :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文