Defining Newline as an Environment Variable does not work as expected

Question:

I have been struggling with different variations and can’t find anything that indicates I am doing something wrong.

csv_data = response["Body"].read().decode('utf-8')
rows = csv_data.split("n")
print(f"Row Count : {len(rows)}")

Row Count : 1000

Now use an env var instead of hardcoded.

source_line_delimiter = os.environ['source_line_delimiter']
print(f"source_line_delimiter : {source_line_delimiter}")

source_line_delimiter : n

csv_data = response["Body"].read().decode('utf-8')
rows = csv_data.split(f"{source_line_delimiter}")
print(f"Row Count : {len(rows)}")

Row Count : 1

csv_data = response["Body"].read().decode('utf-8')
rows = csv_data.split(source_line_delimiter)
print(f"Row Count : {len(rows)}")

Row Count : 1

Update – This works. It is only when the variable is set from the value of an environment variable. I first thought the last end of line was being removed but printing the value showed it contained n as the expected value.

When the variable is defined and set in the .py file, in the example below, the print treats n as a new line and prints an empty string, when set by the environment variable the print prints n as a character string. I guess I am missing a conversion to byte somewhere. I am new to python and coding lambda; this may be elementary :/

Since it works when defined in the py then using "CRLF" or "LF" as the env var and logically branching would work. That is not ideal and would require modification for each delimiter used as opposed to just reading the delimiter from config.

source_line_delimiter = "n"
print(f"Delim = {source_line_delimiter}")

csv_data = response["Body"].read().decode('utf-8')
rows = csv_data.split(source_line_delimiter)
print(f"Row Count : {len(rows)}")

Delim =

Row Count : 1000

The variables are string lieterals and are set in terraform as follows:

environment = {    
    variables = {
        destination_bucket_name = "xxx-${terraform.workspace}",
        source_line_delimiter = "n",
        target_line_delimiter = "n",
        source_column_seperator = ",",
        target_column_seperator = ","

    }
}

I double checked the UI for the variables above and they are set after terraform release.

Asked By: Ross Bush

||

Answers:

If you print(repr(source_line_delimiter)) for the env value, I will all but guarantee you get '\n'. That suggests that it’s not actually treating the env var as a newline, but as a backslash and the literal character n. If that’s accurate, then this should work:

bytes(source_line_delimiter, encoding='utf8').decode('unicode_escape')
Answered By: g.d.d.c
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.