Inputting multiple lines in strings and creating text files in your scripts

20 views
Skip to first unread message

lauri...@gmail.com

unread,
Sep 12, 2013, 3:26:49 PM9/12/13
to

If you know how to use strings enclosed in single or double quotes, such as 'foo' and "foo", you may have noticed that the only way to add newlines is to use the \n character, which would make writing a longer string with newlines one very long line, as in this CSV file:

csv = "Year,Make,Model\n1997,Ford,E350\n2000,Mercury,Cougar"

Though keep in mind in Anduril, CSV files usually have tabs, not commas.

Anduril also has multi-line strings. All other features besides newlines themselves, including string concatenation with the + operator, work identically with them, and you can mix the normal and multi-line strings freely.

Using an actual line break in such strings is a syntax error.

To work around use three pairs of quotes around the string instead. The previous string will become:

csv = """\
Year\tMake\tModel
1997\tFord\tE350
2000\tMercury\tCougar
"""

This allows you to split the string on multiple lines. The newlines become part of the actual string. In the double quote version, you can skip newlines by using a single backslash, \, as the last character on the line - thus you only see the newline on your screen, but not in the actual string, like this:


s = """\
The first line break is skipped to allow aligning lines, e.g. this could be a CSV header line
"""





Inside the three pairs, you can even use either type of quotes as part of the string without escaping, as long as you keep it at sequentially less than three of the enclosing type of quote.

s = """ '''This is allowed''' """ // Becomes '''This is allowed'''
s
= ''' "So is this" ''' // Becomes "So is this"
s
= ''' ''Even this'' '''  // Becomes ''Even this''



You can write any text file to be used as an input to another component by using the StringInput component as follows:

myTextFile=StringInput(content='''

echo "Here go the contents
of this file."

'''
)

BashEvaluate(script=myTextFile)

Reply all
Reply to author
Forward
0 new messages