C
C#7mo ago
Thommy

✅ Reading Text in File at Position x

Hello. I would have to read the text of job_offer_data, starting with target and ending with trailer_place, from the text field. Since the text can always be somewhere different in my text field, the method with a fixed line number doesn't work. Can someone help me with that? With Contains I get up to the line job_offer_data but it's not clear to me how I can output the 13 lines from then on
company : company.volatile.radus_ru.varna {
permanent_data: company.permanent.radus_ru
delivered_trailer: null
delivered_pos: 0
job_offer: 4
job_offer[0]: _nameless.1e8.0c85.9960
job_offer[1]: _nameless.1e8.0c85.af50
job_offer[2]: _nameless.1e8.0c85.b0f0
job_offer[3]: _nameless.1e8.0c85.a590
cargo_offer_seeds: 10
cargo_offer_seeds[0]: 18399998
cargo_offer_seeds[1]: 18399634
cargo_offer_seeds[2]: 18399383
cargo_offer_seeds[3]: 18399334
cargo_offer_seeds[4]: 18400147
cargo_offer_seeds[5]: 18399704
cargo_offer_seeds[6]: 18400015
cargo_offer_seeds[7]: 18399644
cargo_offer_seeds[8]: 18398938
cargo_offer_seeds[9]: 18399418
discovered: true
reserved_trailer_slot: nil
state: 0
state_change_time: 0
}

job_offer_data : _nameless.1e8.0c85.9960 {
target: "canteras_ds.almeria"
expiration_time: 18399337
urgency: 0
shortest_distance_km: 3887
ferry_time: 0
ferry_price: 0
cargo: cargo.emptytank
company_truck: renault_magnum_4x2_a
trailer_variant: trailer.scs_flat_c_r
trailer_definition: trailer_def.scs.flatbed.single_3.container
units_count: 2
fill_ratio: 1
trailer_place: 0
}

company : company.volatile.subse.edirne {
permanent_data: company.permanent.subse
delivered_trailer: null
delivered_pos: 0
job_offer: 2
job_offer[0]: _nameless.1e8.0c85.8440
job_offer[1]: _nameless.1e8.0c85.9480
cargo_offer_seeds: 10
cargo_offer_seeds[0]: 18398603
cargo_offer_seeds[1]: 18399292
cargo_offer_seeds[8]: 18399463
cargo_offer_seeds[9]: 18399170
discovered: true
reserved_trailer_slot: nil
state: 0
state_change_time: 0
}
company : company.volatile.radus_ru.varna {
permanent_data: company.permanent.radus_ru
delivered_trailer: null
delivered_pos: 0
job_offer: 4
job_offer[0]: _nameless.1e8.0c85.9960
job_offer[1]: _nameless.1e8.0c85.af50
job_offer[2]: _nameless.1e8.0c85.b0f0
job_offer[3]: _nameless.1e8.0c85.a590
cargo_offer_seeds: 10
cargo_offer_seeds[0]: 18399998
cargo_offer_seeds[1]: 18399634
cargo_offer_seeds[2]: 18399383
cargo_offer_seeds[3]: 18399334
cargo_offer_seeds[4]: 18400147
cargo_offer_seeds[5]: 18399704
cargo_offer_seeds[6]: 18400015
cargo_offer_seeds[7]: 18399644
cargo_offer_seeds[8]: 18398938
cargo_offer_seeds[9]: 18399418
discovered: true
reserved_trailer_slot: nil
state: 0
state_change_time: 0
}

job_offer_data : _nameless.1e8.0c85.9960 {
target: "canteras_ds.almeria"
expiration_time: 18399337
urgency: 0
shortest_distance_km: 3887
ferry_time: 0
ferry_price: 0
cargo: cargo.emptytank
company_truck: renault_magnum_4x2_a
trailer_variant: trailer.scs_flat_c_r
trailer_definition: trailer_def.scs.flatbed.single_3.container
units_count: 2
fill_ratio: 1
trailer_place: 0
}

company : company.volatile.subse.edirne {
permanent_data: company.permanent.subse
delivered_trailer: null
delivered_pos: 0
job_offer: 2
job_offer[0]: _nameless.1e8.0c85.8440
job_offer[1]: _nameless.1e8.0c85.9480
cargo_offer_seeds: 10
cargo_offer_seeds[0]: 18398603
cargo_offer_seeds[1]: 18399292
cargo_offer_seeds[8]: 18399463
cargo_offer_seeds[9]: 18399170
discovered: true
reserved_trailer_slot: nil
state: 0
state_change_time: 0
}
14 Replies
Mayor McCheese
Mayor McCheese7mo ago
Is this a school assignment or something?
Thommy
Thommy7mo ago
its for me, not for school to old for school ^^
Mayor McCheese
Mayor McCheese7mo ago
You have to know the file position to seek to, you can't really get position from line number Structured formats exist, like json, yaml, parquet, etc Some of them can be read "forward only", or a bit at a time
Thommy
Thommy7mo ago
ok, i read the file with File.ReadLines
Mayor McCheese
Mayor McCheese7mo ago
Iirc ReadLines is an enumerable?
Thommy
Thommy7mo ago
You could try to get the line number with a for loop and then read out 13 lines from there?
Mayor McCheese
Mayor McCheese7mo ago
Yeah, something like that could work But it's fragile It'll work until the format changed
Thommy
Thommy7mo ago
The format of this text block always remains the same, only the position changes
Mayor McCheese
Mayor McCheese7mo ago
So really rather than 13 lines to want to read to your next marker
Thommy
Thommy7mo ago
I'll test it tomorrow and if I get stuck, I'll let you know
Mayor McCheese
Mayor McCheese7mo ago
kk if the fields are unique you can treat them like kvp's ( key: value pairs ) and store them in a dictionary and read them to your hearts content something like....
file.ReadLines().ToDictionary(k=>k.SubString(0, k.IndexOf(':')), v=>v.SubString(v.IndexOf(':')+1)));
file.ReadLines().ToDictionary(k=>k.SubString(0, k.IndexOf(':')), v=>v.SubString(v.IndexOf(':')+1)));
you have duplicate keys though so then you're reading like a for loop like you suggested.
Bailey
Bailey7mo ago
a sollution could be: read complete file split it into an array (on the line) Go through it with a for next or use a ling with contains
Accord
Accord7mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Thommy
Thommy7mo ago
Ok, i found an solution!
List<string> datei = File.ReadLines(fileName).Skip(startingline).Take(readlines).ToList();
List<string> datei = File.ReadLines(fileName).Skip(startingline).Take(readlines).ToList();
it read the lines at startingline and read 'readlines' from the file and take it to an List string
Want results from more Discord servers?
Add your server
More Posts