Python 从外部程序获取数据

Python 从外部程序获取数据,python,Python,我需要一个从外部编辑器获取数据的方法 def _get_content(): from subprocess import call file = open(file, "w").write(some_name) call(editor + " " + file, shell=True) file.close() file = open(file) x = file.readlines() [snip] for line

我需要一个从外部编辑器获取数据的方法

def _get_content():
     from subprocess import call
     file = open(file, "w").write(some_name)
     call(editor + " " + file, shell=True)
     file.close()
     file = open(file)
     x = file.readlines()

     [snip]
for line in open("file"):
    print "editing line ", line
    # eg replace strings
    line = line.replace("somestring","somenewstring")
    print line
我个人认为应该有一种更优雅的方式。你看,我需要与外部编辑器交互并获取数据

你知道更好的方法/有更好的想法吗

编辑:

Marcelo让我想到了使用tempfile来做这件事

我是这样做的:

def _tempfile_write(input):
    from tempfile import NamedTemporaryFile

    x = NamedTemporaryFile()
    x.file.write(input)
    x.close()
    y = open(x)

    [snip]

这项工作做得不错,但也不太令人满意。听说过产卵吗?。

所有程序都是这样做的,好吧。当然,我使用的所有版本控制系统都会创建一个临时文件,将其传递给编辑器,并在编辑器退出时检索结果,就像您所做的那样。

这是所有程序都会这样做的,好吧。当然,我使用的所有版本控制系统都会创建一个临时文件,将其传递给编辑器,并在编辑器退出时检索结果,就像您所做的那样。

编辑器只允许您以交互方式编辑文件。您还可以使用Python编辑文件。不需要调用外部编辑器

def _get_content():
     from subprocess import call
     file = open(file, "w").write(some_name)
     call(editor + " " + file, shell=True)
     file.close()
     file = open(file)
     x = file.readlines()

     [snip]
for line in open("file"):
    print "editing line ", line
    # eg replace strings
    line = line.replace("somestring","somenewstring")
    print line

编辑器只允许您以交互方式编辑文件。您还可以使用Python编辑文件。不需要调用外部编辑器

def _get_content():
     from subprocess import call
     file = open(file, "w").write(some_name)
     call(editor + " " + file, shell=True)
     file.close()
     file = open(file)
     x = file.readlines()

     [snip]
for line in open("file"):
    print "editing line ", line
    # eg replace strings
    line = line.replace("somestring","somenewstring")
    print line

我建议使用列表,而不是字符串:

def _get_content(editor, initial=""):
    from subprocess import call
    from tempfile import NamedTemporaryFile

    # Create the initial temporary file.
    with NamedTemporaryFile(delete=False) as tf:
        tfName = tf.name
        tf.write(initial)

    # Fire up the editor.
    if call([editor, tfName]) != 0:
        return None # Editor died or was killed.

    # Get the modified content.
    with open(tfName).readlines() as result:
        os.remove(tfName)
        return result

我建议使用列表,而不是字符串:

def _get_content(editor, initial=""):
    from subprocess import call
    from tempfile import NamedTemporaryFile

    # Create the initial temporary file.
    with NamedTemporaryFile(delete=False) as tf:
        tfName = tf.name
        tf.write(initial)

    # Fire up the editor.
    if call([editor, tfName]) != 0:
        return None # Editor died or was killed.

    # Get the modified content.
    with open(tfName).readlines() as result:
        os.remove(tfName)
        return result

你的问题很模糊。你到底想实现什么?你觉得这种方法有什么不好的地方?我是否需要用户输入一些文本并将其作为字符串?我是否需要用户编辑预先存在的文件?你是在问如何生成一个新的编辑器进程还是如何从用户那里获取输入?我说的是从用户那里获取输入我承认丑陋不是正确的词。。。也许会说,如果有,我正在寻找一种更优雅的方法。你的问题很模糊。你到底想实现什么?你觉得这种方法有什么不好的地方?我是否需要用户输入一些文本并将其作为字符串?我是否需要用户编辑预先存在的文件?你是在问如何生成一个新的编辑器进程还是如何从用户那里获取输入?我说的是从用户那里获取输入我承认丑陋不是正确的词。。。也许会说,如果有,我正在寻找一种更优雅的方式来做这件事。提到临时文件很好。。我在那个名为tempfile的文件上找到了一个很好的Python模块。我觉得这听起来很好。提到临时文件很好。。我在那个名为tempfile的文件上找到了一个很好的Python模块。我觉得这听起来不错,是的,我知道。虽然我需要获得用户输入,然后立即处理这些数据,并将其全部存储在数据库中,否则我会这样做:是的,我知道。虽然我需要获得用户输入,然后立即处理这些数据,并将其全部存储在数据库中,否则我会这样做:Gah,忘记了对正:您想使用一个列表来调用,shell=False,因为这样您就不必担心转义文件名空间中的任何字符,&;,外壳赋予的特殊含义。诚然,NamedTemporaryFile不应该给您一个包含这些字符的文件名,但是进入.Gah是一个好习惯。忘记了理由:您想使用一个列表来调用,shell=False,因为这样您就不必担心转义文件名空间中的任何字符,&;,外壳赋予的特殊含义。诚然,NamedTemporaryFile不应该为您提供包含这些字符的文件名,但这是一个好习惯。