# Using QGIS Field Calculator on delimiter separated values

I attached a picture of my attribute table. I got there a field named "funktion", the values are given in volts but i need kilovolts. So I created a new field and defined: funktion = funktion /1000. The problem is some fields are like this " 380000;220000;110000 ", so my approach does not work. Does anybody know how to solve it? I imagine with a python command it should be possible, but I dont know the correct one.

• Of what type is your column? Cause i think you shouldnt be able to use a ";" in a column of type integer or decimal. And if i am right and it is of type string, you cant make calculations on that column Sep 21, 2022 at 7:05
• That seems to be the OSM powerline data @Nunkuat. It is noted this way, because sometimes a transmission line may bear cables with two or even three different voltages. Hence, the field type is string.
– Erik
Sep 21, 2022 at 7:22
• What should be the result for the column "380000;220000"? Sep 21, 2022 at 7:41

You need to use some functions to first split the volts values to an array and then do the calculations for each value and then merge the array back to a string.

``````array_to_string(
array:=array_foreach(
array:=string_to_array(
string:=volts,
delimiter:=';'
),
expression:=@element/1000
),
delimiter:=';'
)
``````

The expression can be written also without the variable names:

``````array_to_string(
array_foreach(
string_to_array(volts, ';'),
@element/1000
),
';'
)
``````

The string "380000;230000" needs to be split at the ";", each part converted to integer and divided by 1000, that then needs to be converted back to string and joined by ";" again.

``````";".join(list(map(lambda x: str(int(int(x)/1000)),s.split(";"))))
``````

or (thanks, @LauriK)

``````";".join(str(int(voltage) / 1000) for voltage in s.split(";"))
``````

where `s` is the input string.

In the field calculator, input this in the "Funktionseditor" (function editor) tab and click the load/save functions button:

``````@qgsfunction(args='auto', group='Custom')
def powerline_divide(s, feature, parent):
"""
Divides powerline voltage strings by 1000.
"""
return ";".join(list(map(lambda x: str(int(int(x)/1000)),s.split(";"))))
``````

Then change to the expression tab and use `powerline_divide( "funktion" )` as expression, where funktion is the attribute name (it needs the double quotes).

• Good to have also a python alternative answer. The calculation could though be simplified by using comprehension like: `return ";".join(str(int(voltage) / 1000) for voltage in column_value.split(";"))` Sep 21, 2022 at 10:54
• Thank you! this is great. Sep 23, 2022 at 11:10