Select to view content in your preferred language

line break error in calculate field

1573
4
09-23-2012 05:39 AM
PaulGoede
Emerging Contributor
Hey people!

I have a problem while working in the code block of the calculate field process in model builder.
As you can see in my code below, I want to add a line break in the newly created txt-file.
I tried to add it through a "\n" in the line:
        f.write("All Postcodes \n")


But everytime I enter "\n", it ist directly changed into a strange character which looks like a square.

Purpose: I want a line break to be inserted in the textfile.
What the process does: It inserts the strange square-character instead of a line break.

I've tried the double backslash "\\n" but it doesn't work.

import os, csv

def plzToFile(csvfilepath, csvfilename, plz):
    ext = 'txt' # Defines output file extension as txt
    
    # Join CSV file path and name, adding extension if necessary
    csvfile = os.path.join(csvfilepath, os.extsep.join((csvfilename, ext)) if not os.path.splitext(csvfilename)[1].lower().endswith(ext) else csvfilename)

    # Open text file for writing
    with open(csvfile, 'wb') as f:
        w = csv.writer(f)
        rows = map(lambda x: x.split(';'), [plz]) # Transpose the semicolon-delimited values into rows
        f.write("All Postcodes \n")
        w.writerows(rows)
    return csvfile


I appreciate your help!
Tags (2)
0 Kudos
4 Replies
JasonScheirer
Esri Alum
Are you viewing the text file in Notepad? Windows usually uses \r\n (and not just \n) in most places for its line break marker. Some text editors will interpret both as a new line, but they are in the minority. The solution is just "Hello\r\n" instead of "Hello\n"
0 Kudos
PaulGoede
Emerging Contributor
Hello Jscheirer,

thanks for your suggestion.

I insert the code directly into the code block of the process-dialogue of "calculate field" in the model builder.
Whenever I type "\n" (without the quotation marks), it is transformed to the square character immediately, just when I click on another part of the dialogue or close the window. That's my problem.
[ATTACH=CONFIG]17907[/ATTACH] (see here how the \n looks like after the transformation)

Do you have any other suggestions?

I thought about exporting the whole model to python script. The result is a file somewhere on my disk. How do I reinsert the code of that file to ArcGIS by means of making the code an executable tool in ArcGIS?
0 Kudos
JasonScheirer
Esri Alum
I have never, ever heard of that behavior occurring at all in the UI. What version of ArcGIS are you using?

I suppose it would make sense to export your model (or at least just this part) to a Python script.
0 Kudos
PaulGoede
Emerging Contributor
Hi,

I have ArcGIS 10 installed.I also have another version (the student version) of ArcGIS 10 installed on another computer.

I'm sorry I confused "field calculater" and "calculate value"

What I described above happens on both computers. Try it yourself: Open the Calculate Value (not the field calculator) in model builder, click in the code block and type "\n". Then click e.g. on "expression" above. You will see the "\n" immediately change to a square character.

before:
[ATTACH=CONFIG]17938[/ATTACH]
after:
[ATTACH=CONFIG]17939[/ATTACH]
0 Kudos