Question

Right now I'm programming a python script to read data out of a binary format (netCDF) and csv files out of it. Only to read it again from another program which only accepts csv files.

Is there any option where I can give the in-memory CSV-File directly to the second program? Some kind of pseudo-file? Where the second program thinks it is reading a CSV file but actually just in-memory file?

Was it helpful?

Solution

You can use the subprocess module's piping features to write the data to the other program.

import subprocess
proc = subprocess.Popen("/usr/bin/tac", stdin=subprocess.PIPE)  # Output will go to stdout
proc.communicate("hello\nworld\n")  # This data goes to the subprocess.

will output (or rather, tac will)

world
hello

EDIT: This is assuming the other program supports reading from standard input. If it doesn't, you could probably simulate this with fifos on UNIX-like systems...

OTHER TIPS

you can use tempfile module:

>>> import tempfile
>>> f = tempfile.NamedTemporaryFile(delete=False)
>>> f.write("Hello World!\nHello World!\n")
>>> f.close()
>>> f=open(f.name)
>>> f.readlines()
['Hello World!\n', 'Hello World!\n']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top