0

Firstly, I have to tell you that I'm an absolute noob at python and started using it hoping to be able to create small executable files for basic engineering tasks. For example, the below code calculates the tracing coordinates for a progressive curve for railways:

l=int(input('l='))
r=int(input('r='))
for x in range(0,l+1,2):
    y=round(((x**3)/(6*l*r)),4)
for x in range(0,l+1,2):
    grafic=print(x,",",round(((x**3)/(6*l*r)),4),sep='')

with the output, for given variables values:

l=42
r=195
0,0.0
2,0.0002
4,0.0013
6,0.0044
8,0.0104
10,0.0204
12,0.0352
14,0.0558
16,0.0834
18,0.1187
20,0.1628
22,0.2167
24,0.2813
26,0.3577
28,0.4467
30,0.5495
32,0.6668
34,0.7998
36,0.9495
38,1.1166
40,1.3024
42,1.5077

I'm trying to create a ".scr" or ".txt" file from this result which must be:

pline
0,0.0
2,0.0002
4,0.0013
6,0.0044
8,0.0104
10,0.0204
12,0.0352
14,0.0558
16,0.0834
18,0.1187
20,0.1628
22,0.2167
24,0.2813
26,0.3577
28,0.4467
30,0.5495
32,0.6668
34,0.7998
36,0.9495
38,1.1166
40,1.3024
42,1.5077

The ideea is to creat a script file which can be imported in ACAD for example, the output beeing that it traces a 2D polyline. Any help would be greatly appreciated!

joaquin
  • 82,968
  • 29
  • 138
  • 152

2 Answers2

0

You can create a file with the built-in open function from Python:

with open('output.txt', 'w') as outfile:
    outfile.write("pline\n")
    l = int(input('l='))
    r = int(input('r='))
    for x in range(0, l + 1, 2):
        y = round(((x ** 3) / (6 * l * r)), 4)
    for x in range(0, l + 1, 2):
        outfile.write("{},{}\n".format(x, round(((x ** 3) / (6 * l * r)), 4)))

Output

pline
0,0.0
2,0.0002
4,0.0013
6,0.0044
8,0.0104
10,0.0204
12,0.0352
14,0.0558
16,0.0834
18,0.1187
20,0.1628
22,0.2167
24,0.2813
26,0.3577
28,0.4467
30,0.5495
32,0.6668
34,0.7998
36,0.9495
38,1.1166
40,1.3024
42,1.5077

The function outfile.write writes the text to the file, in the example above outfile. To write a line you must append the newline character \n at the end of each string you write.

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
0

you can use write

  1. open your text file as textfile

  2. use \n for new line

  3. use textfile.write

Raj
  • 707
  • 6
  • 23