2

Context

Using FME, I want to read a text file containing several lines of variables I already defined, given in the following format:

var_name = "var value"

There are many lines, each one contains one single variable with one single value:

var_name0 = "var value0"
var_name1 = "https://path-to-API/v1"
var_name2 = "https://path-to-other-API/v1"
trigger1 = 1
trigger1 = 0
path_shapefiles = "/path/to/some/shapefiles_folder/"
path_geopackages = "/path/to/some/geopackages_folder/"
...

For the moment, I read the text file using an AttributeFileReader to which I connect an AttributeSplitter.

My results come as one single concatenated line in the output, with absolutely no separators which is obviously not what I want, such as:

var_name0 = var value0var_name1 = "https://path-to-API/v1"var_name2 = "https://path-to-other-API/v1"...

In the "Delimiter or Format String" field of the AttributeSplitter, I tried inserting \n \r or\r\n without any success:

Example image

I still got one unique row with all attributes concatenated together.

I also tried inserting a single newline by pressing one time the Enter key in the "Editor" as discussed here (2013): https://knowledge.safe.com/questions/2361/use-attributesplitter-with-a-carriage-return.html without much success.

Question

Do you know what I am missing or what I'm doing wrong with that? Or any kind of workaround to end up with a table which fields are the variables in the value along with their value on the first row for example?

Environment

I'm using FME(R) 2020.0.0.1 (20200316 - Build 20202 - linux-x64) on Ubuntu 18.04.

3 Answers 3

2

Your final comment on @neilsgerrit's post was on the right track. You're not getting two lists. You're getting one list with two values.

I suggest using a text reader where you read the contents line by line. Then use an AttributeSplitter to split on the = sign. This produces a list with two values - the variable name and the variable value.

Then, you can use an AttributeManager to rename the values of _list{0} and _list{1} to VariableName and VariableValue respectively (or whatever you want these attributes to be called). enter image description here

Your workspace would look something like this: enter image description here

You could also use a ListIndexer, but I think the AttributeManager is easier.

2

Actually, I think the easiest way to read this is with the CSV format reader, using the = character as the delimiter, like so:

enter image description here

I think this works better partly because it's just a single step, but mostly because if you had an = character in the variable value, like:

mytest = "a = 123"

it wouldn't treat that as a new separator, because the value is enclosed in quotes. I doubt that the AttributeSplitter would do the same.

You'd also want to be careful to remove leading or trailing space characters (use the AttributeTrimmer transformer) but you'd want to do that anyway, even if you use the AttributeSplitter.


Actually, now that I test it:

mytest = "a = 123"

...doesn't work because of the spaces surrounding the = delimiter character. So if you could remove those from the original file, it would work perfectly:

enter image description here

But that was just an added bonus anyway. I'll still file an enhancement request to ignore spaces surrounding the delimiter, because I think that would be a useful capability.

0

The attributesplitter cant do regex. Replace the \n using the stringreplacer with another unique character like a pipe or a @ then feed it to the attributesplitter.

3
  • I still get one unique field in the output of the AttributeSplitter, even after having replaced the newline char by & using a StringReplacer and using that new character as the Delimiter in the AttributeSplitter options. Apr 24, 2020 at 16:34
  • Do you read the file as whole (one feature) or per line (multiple features)? You can set this in the text reader parameters. It should be as whole. Apr 24, 2020 at 16:54
  • Actually the AttributeFileReader doesn't have such option. But you put me on another track; i.e. reading the file using a reader, where, indeed, I can read it "line by line" (if I set the parameter to read the whole file at once, I end up in the same situation than with the AttributeFileReader). And with a single column with N lines, I can now use the AttributeSplitter on the = sign. But then I end up with 2 lists _llist{0} and _list{1} which would be nice to transpose actually, so that each parameter of the text file end up in its own column, with its values in the single row. Apr 24, 2020 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.