timo-71

Рейтинг
63
Регистрация
19.09.2018
VoV @:
IMHO, only brute force characters will

Solutions above was with stream_get_line, a pack of characters to the separator + preg_match. one character reading makes it easier to solve the problem.

Especially if slightly complicate the task. The json

 [
  { 
"Id": 2234,
"Scores": {
"Max": 267,
"Min": 17
}
}
{
"Id": 2235
"Name": "OOOYO",
"Price": 200319.12,
"Scores": {
"Max": 408,
"Min": 78
}
}
And we must find scores.max, for example more than 500, and the object format is flexible

Slightly changing the code


$ Read = 0;
......
if ($ c == '{') {
$ Read ++;
}
if ($ read> 0) {
. $ Str = $ c;
}
if ($ c == '}') {
$ Read--;

problem is easily solved

------------

Danforth:
or you can drive and zadalbyvat core siskolami reading 1 byte.

Yes, constructive comments, but 20 minutes working hard to jot down. To me at least. :(

png json-2.png
Sly32:
content = data.read()

В задаче запрещено грузить весь файл

danforth:
Загружать весь файл полностью и распаковывать в массив нельзя.
Sly32:
content = data.read ()

The problem is forbidden to ship the entire file

Danforth:
Download the entire file completely and unpack the array can not.

<?php
header('content-type:text/plain');
$handle = fopen( '/var/web/aio/data/json', 'cb+' );
$str = ''; $read = false;
$t0 = microtime(true);
$i = 0;
$ii = 0;
while (!feof($handle)) {
$c = fread($handle, 1);
if($c=='{'){
$read = true;
}
if($read) {
$str.=$c;
}
if($c=='}') {
$a = json_decode($str);
$i++;
if( is_object($a) && $a->scores > 0.7){

$ii++;
$a->allobjcnt = $i;
$a->matchedobjcnt = $ii;
print_r($a);
}
$str = ''; $read = false;
}
}
echo sprintf("Timing: %0000001.4f sec", (microtime(true) - $t0)), "
######################################################
# /ru/forum/1032923
######################################################


";
fclose($handle);
?>

Про запись в результата файл сразу не заметил.

Но там несложно, если пустой просто пишем "[{json_encode найденного}]"

дальше идем в конец, находим позицию "]" и с этой позиции дописываем ",{json_encode найденного}]"

$p = fseek($handle, -1, SEEK_END);
$c = ''; $pos = ftell($handle);
while( $c!=']' ){
$c=fread( $handle , 1 );
$pos-=2;
fseek($handle, $pos);
}

 <? Php 
header ( 'content-type: text / plain');
$ Handle = fopen ( '/ var / web / aio / data / json', 'cb +');
$ Str = ''; $ Read = false;
$ T0 = microtime (true);
$ I = 0;
$ Ii = 0;
while (! feof ($ handle)) {
$ C = fread ($ handle, 1);
if ($ c == '{') {
$ Read = true;
}
if ($ read) {
. $ Str = $ c;
}
if ($ c == '}') {
$ A = json_decode ($ str);
$ I ++;
if (is_object ($ a) && $ a-> scores> 0.7) {

$ Ii ++;
$ A-> allobjcnt = $ i;
$ A-> matchedobjcnt = $ ii;
print_r ($ a);
}
$ Str = ''; $ Read = false;
}
}
echo sprintf ( "Timing:% 0000001.4f sec", (microtime (true) - $ t0)), "
################################################## ####
# / Ru / forum / 1032923
################################################## ####


";
fclose ($ handle);
?>

Pro record in the results file is not immediately noticed.

But there is easy, if a blank simply write "[{json_encode found}]"

then go to the end, we find the position of the "]" and from that position we finish ", {json_encode found}]"

 $ P = fseek ($ handle, -1, SEEK_END); 
$ C = ''; $ Pos = ftell ($ handle);
while ($ c! = ']') {
$ C = fread ($ handle, 1);
$ Pos- = 2;
fseek ($ handle, $ pos);
}
Sitealert:
Он утверждает, что это очень важная задача

Да ладно, шутит наверное.

Sitealert:
Да нет же. Он получает данные из базы данных

Вообще не суть откуда. Рекурсия, это к тому что могло бы быть и так в задаче:

{
'id': 0,
'media_category': 'clip',
'child' : {
'id': 22,
'media_category': 'clip',
'child' : {
'id': 27,
'media_category': 'clip',
'child' : {
'id': 77,
'media_category': 'xxx',
}
}
}
}
Sitealert:
He argues that this is a very important task

Come on, probably joking.

Sitealert:
No. It receives data from the database

 It never is coming from. Recursion, is to ensure that it could have been so in the problem: 
{
'Id': 0
'Media_category': 'clip',
'Child': {
'Id': 22,
'Media_category': 'clip',
'Child': {
'Id': 27,
'Media_category': 'clip',
'Child': {
'Id': 77,
'media_category': 'xxx',
}
}
}
}
Gerga:
Лучше как можно раньше отсечь ненужные данные...

Sly32 задал абстрактный вопрос - задачу для первоклассника. Не суть как эта задача появляется. Это может быть рекурсивно. Ну вот, например, на таком объекте:

db.runCommand({'collStats':'ttu'})
{
"ns" : "teсh.ttu",
"size" : 843057200,
"count" : 3367400,
"avgObjSize" : 250,
"storageSize" : 90828800,
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
}
.................
"totalIndexSize" : 641056768,
"indexSizes" : {
"_id_" : 355459072,
"name_text_text_text" : 285597696
},
"scaleFactor" : 1,
"ok" : 1

Например не удалять, а оставить "bytes currently in the cache" > xxxx

Оно там есть где то в .......... удаленном


"wiredTiger" : {
"cache" : {
"bytes currently in the cache" : 108254,

----------


Вы ввели слишком длинный текст (37822 символов). Пожалуйста, сократите его до 12000 символов.

Сократил как мог

Gerga:
Better as soon as possible to cut off the unnecessary data ...

Sly32 asked the abstract question - challenge for a first grader. Not the essence of this task is displayed. This can be recursive. Well, for example, at such a facility:

 db.runCommand ({ 'collStats': 'ttu'}) 
{
"Ns": "tesh.ttu"
"Size": 843057200,
"Count": 3367400,
"AvgObjSize": 250,
"StorageSize": 90828800,
"Capped": false,
"WiredTiger": {
"Metadata": {
"FormatVersion": 1
}
.................
"TotalIndexSize": 641056768,
"IndexSizes": {
"_Id_": 355459072,
"Name_text_text_text": 285597696
}
"ScaleFactor": 1,
"Ok": 1

For example do not remove and leave "bytes currently in the cache"> xxxx

It is there somewhere in the remote ..........


"WiredTiger": {
"Cache": {
"Bytes currently in the cache": 108254,

----------


You entered too much text (37822 characters). Please reduce it to 12,000 characters.

He reduced as he could

onep:
посмотреть пример сортировки массива с алфавитно-цифровыми

Огромный плюс питона, что есть все на все случаи жизни (pip install natsort)

import natsort

u = natsort.natsorted(u)

['AirPods',
 'Apple Pencil',

'Apple Watch',
'MacBook',
'MacBook Air',
'MacBook Air 13.3-inch (2018)',
'MacBook Air 13.3-inch with Retina Display (2018)',
'MacBook Pro',
'iPad 2',
'iPad 3',
'iPad 4',
'iPad 9.7',
'iPad 9.7 (2018)',
'iPad 10.2 (2019)',
'iPad Air',
'iPad Air 2',
'iPad Air (2019)',
'iPad Mini',
'iPad Mini 2',
'iPad Mini 3',
'iPad Mini 4',
'iPad Mini (2019)',
'iPad Pro 9.7',
'iPad Pro 10.5',
'iPad Pro 11-inch',
'iPad Pro 11-inch (2020)',
'iPad Pro 12.9',
'iPad Pro 12.9 (2017)',
'iPad Pro 12.9 (2018)',
'iPad Pro 12.9-inch (2020)',
'iPhone 4',
'iPhone 4s',
'iPhone 5',
'iPhone 5c',
'iPhone 5s',
'iPhone 6',
'iPhone 6 Plus',
'iPhone 6s',
'iPhone 6s Plus',
'iPhone 7',
'iPhone 7 Plus',
'iPhone 8',
'iPhone 8 Plus',
'iPhone 11',
'iPhone 11 Pro',
'iPhone 11 Pro Max',
'iPhone SE',
'iPhone SE 2',
'iPhone X',
'iPhone XR',
'iPhone XS 5.8-inch',
'iPhone XS Max',
'iPod Touch 5',
'iPod Touch 6',
'iPod Touch (2019)']
Всего: 541