Getting output from pane to output.txt files

Hello, I’ll preface this by saying that I only joined Replit a couple hours ago, as recommended by a Reddit user to aid with some basic text editing. I tried replicating what they did in the python script but that wasn’t working so I abandoned that and forked the original one. That is working… somewhat better? I cut out the data.txt with my own new block of coordinates, added a files named ‘output.txt’, ran the script, the newly edited coordinates came out fine in the output pane, but didn’t convey over to the output.txt for probably a variety of reason due to my noob status with this whole thing. Then, even once I’ve gotten the coordinates into the output.txt, there spacing needed to work with GeoJSON is absent, which I don’t know how to fix.

So to summarize: How do I get the output from the pane to the .txt?

https://replit.com/@GavinFoley2/text7123#main.py

code snippet

Welcome to the forums!

I don’t understand what you’re trying to do here. Are you trying to copy the console output then paste it to output.txt?

2 Likes

I believe so. I am trying to get the output from running the script into the the output.txt, if that makes any sense.

Why not change res to a string, concatenate the coords and extras, and re-write output.txt by doing

with open("output.txt") as o:
  o.write(res)

?

2 Likes

Hey @GavinFoley2, welcome!

You can just set the file to print to:

with open("data.txt") as d:
    text = d.read()

coords = text.split(":")
res = []
for c in coords:
    lon, lat = c.split(",")
    res.append([float(lat), float(lon)])

with open("output.txt", "w") as file:
    print("[\n\t", end="", file=file)
    print(*(res[::-1]), sep=",\n\t", file=file)
    print("]", file=file)

Or if you’d like to save it to a file and print it to the Console:

Note: the following was written by Phind, an AI.

Here is a general method how you can Save stdout to a File: You can use the built-in sys module to redirect the stdout to a file:

import sys

# Open the file in write mode.
with open('output.txt', 'w') as f:
    # Backup original stdout stream.
    original_stdout = sys.stdout

    # Set stdout to the file object.
    sys.stdout = f

    # Print something (it goes to the file).
    print('This line will be written to the file.')

    # Restore original stdout stream.
    sys.stdout = original_stdout

# At this point, anything printed to stdout goes to console.
print('This line is printed to the console.')

This will write the text “This line will be written to the file.” to the file file.txt.

These are popular methods, but remember to be careful when changing sys.stdout, as it’s a global and may affect other parts of your program if not handled properly. You should always ensure you restore the original stdout.

Back to me, a hooman/penguin

You can modify your code to look like this:

import sys


# Define a function to print the data from data.txt
def printData():
    with open("data.txt") as d:
        text = d.read()

    coords = text.split(":")
    res = []
    for c in coords:
        lon, lat = c.split(",")
        res.append([float(lat), float(lon)])

    print("[\n\t", end='')
    print(*(res[::-1]), sep=",\n\t")
    print("]")


# Open the file in write mode.
with open('output.txt', 'w') as f:
    # Backup original stdout stream.
    original_stdout = sys.stdout

    # Set stdout to the file object.
    sys.stdout = f

    # Print something (it goes to the file).
    printData()

    # Restore original stdout stream.
    sys.stdout = original_stdout

# Print the data to the Console too,
printData()
3 Likes