What are the rules for the input files? How is the data format specified?
How does it need to look like in JSON?
Just wrapping your input files text in quotes and replacing newlines with literal \n would make it valid JSON. Though that probably isn’t what you need.
Also, if you show some code that shows your current state, and explain what it does instead of what it should do, increase your chance for answers.
Hi i dont have the code i was asked to use map and slices. I really need way to this . Please kindly help with code. I don’t have any other information left to give.
Secondly: It is unlikely that anyone on this site will write an entire program that solves your whole problem and give it to you. There are other web sites where you can pay people to do that for you, but the goal of the members on this site is to help guide you so you can solve the problem (mostly) yourself.
With that goal in mind, at this time, my suggestions to you are:
Please confirm the format of the input (separate .txt files?) and output (separate .json files?). Knowing exactly what you start with and what you want to end with will help us guide you better.
Write down the steps that the code will have to execute to do what you want. You do not have to write them down in Go; you can use “pseudo-code” or English, or whatever other language you are comfortable with.
Try writing the code for each step. If you get stuck, it is at this point that the members of this forum will be able to help you more directly, and possibly with code. For example:
New question: “I have multiple .txt files like ‘sample input 1.txt,’ ‘sample input 2.txt,’ etc., and I want to open each one in a loop, but I don’t know how many there could be. Maybe up to ‘sample input 10.txt’ or ‘sample input 100000.txt.’ How do I find all the files? Here’s my code so far: …”
For a question like this, you could potentially receive several answers from several members on this forum, some of which may contain code. This question does not ask anyone to write the code for you; it is a request for guidance after you have thought about the problem and you are stuck.
Yes your input and output is right. Input is in 1 .txt format file and output is is in one .json format.
I will try to code it but here pseudo how it should go around
Package Kingdom
import( necessary import files.
)
Const comand =“Topics to explore”
Type output struct{
Unpaarsedlines []string json:"unpaarsedlines,omiempty"
Mammals map[string][] json:"mammals"
}
This mammals should have birds and animals.
The thing with input is that it can increase its not fixed. Like
Sample input 1-
Topics to explore
Birds
Blaha blaha
Sample input 2-
Topics to to explore
Birds
Species
Description Black
Blaha. Blaha
Sample input 3-
Topics to explore
Animals
Blah blah
Description Black
Blah blah
In my question, I asked if the input and output were separate files, but your answer seems to imply that the input and output are each a single file. Is there one input file and one output file?
If so, it looks to me like the separator between each record in the input file is “Sample input n-” where n is an incrementing number. Is that true?
Can you elaborate on the relationship between
Type output struct{
Unpaarsedlines []string json:"unpaarsedlines,omiempty"
Mammals map[string][] json:"mammals"
}
It looks like the { "Animals": [ ... ] } part might correspond to the Mammals field in the output struct, but the type definition isn’t complete: You have map[string][] ← but you need a type name here.
Hi
Yes there is only one single input file and one single output file named as inpu.txt and output.json respectively.
Coming to later part the birds or animals aren’t fixed it can change to anything like humans too. So the basic idea was to use mammals map[string][]string and store it as dictionary with slices.
Something like this
Mammals : {
“Human” :{
Species,
Description Black,
Blaha blaha
},
And so on
}
So it sounds like if your input.txt file looks exactly like this:
Sample input 1-
Topics to explore
Birds
Blaha blaha
Sample input 2-
Topics to to explore
Birds
Species
Description Black
Blaha. Blaha
Sample input 3-
Topics to explore
Animals
Blah blah
Description Black
Blah blah
Then you want output.json to look exactly like this:
I’ve assumed that if you have two Sample input n- sections with the same “key” (“Birds”, “Animals”, etc.) under “Topics to explore,” then the subsequent values (“Species”, “Description Black”, etc.) should just be appended to. For example, both your “Sample input 1-” and “Sample input 2-” sections say “Birds,” so I assumed 1’s value, “Blaha blaha” should be first and 2’s values: “Species”, “Description Black”, and “Blaha. Blaha” should be appended after.