It is distributed here (https://github.com/nendo-code/rpttxt).
For example, suppose you want to create data like this.
output.txt
{
"id": 1,
"name": "John",
"age": 21
}
{
"id": 2,
"name": "Paul",
"age": 35
}
{
"id": 3,
"name": "Tom",
"age": 64
}
What would you do if you created this data? I think it would be like this if I could do it manually.
First, make one item.
step1.txt
{
"id": 1,
"name": "John",
"age": 21
}
Copy and paste as many items as you need.
step2.txt
{
"id": 1,
"name": "John",
"age": 21
}
{
"id": 1,
"name": "John",
"age": 21
}
{
"id": 1,
"name": "John",
"age": 21
}
3. Rewrite the value of each item to the target value.
step3.txt
{
"id": 1,
"name": "John",
"age": 21
}
{
"id": 2,
"name": "Paul",
"age": 35
}
{
"id": 3,
"name": "Tom",
"age": 64
}
You can do it manually like this, but if there are many cases, it is easy to make mistakes. I thought it would be nice if data could be written separately for data and templates for templates. So
Write a template
template.txt
{
"id": {No},
"name": "{Name}",
"age": {Age}
}
Write the data in csv (or export it in csv with Excel etc.)
data.csv
{No},{Name},{Age}
1,John,21
2,Paul,35
3,Tom,64
I created a program that gives the following output when I feed them. That is rpttxt.
output.txt
{
"id": 1,
"name": "John",
"age": 21
}
{
"id": 2,
"name": "Paul",
"age": 35
}
{
"id": 3,
"name": "Tom",
"age": 64
}
I felt that there might be a tool similar to the one in the world, but I couldn't find the one with the desired operation feeling, so I reinvented the wheel. Also, I read the book "The idea of UNIX-its design concept and philosophy" and thought it was wonderful, so I made it a command line tool with a simple interface like that.
(Example) Execution on the command line
$ python3 rpttxt.py template.txt data.csv > output.txt
I wrote it in python3 and made the binary for Windows with PyInstaller. PyInstaller is an easy and very good tool. Also, since it is dull to start the command line on Windows, I also added a batch file to convert in one shot as a bonus.
Recommended Posts