0

编辑:这个问题没有经过深思熟虑。我的问题实际上是简单地尝试通过闪亮显示文本 - 不存储它。有时候你只是想不清楚。

我有一个 python 脚本,可以将一些结果打印到标准输入,我想将这些结果读入 R 中的字符向量。这通常可以很好地使用system(...,intern=TRUE),但是在这种情况下,当添加转义字符时它对我不起作用(脚本返回HTML 和添加转义字符会导致 HTML 格式错误)。我可以通过将 python 的输出保存到一个临时文件中并将该文件读入 R 来解决这个问题,但如果有一个我想不到的简单修复方法,我宁愿避免这种情况。这是我的意思的一个例子:

> #f.py is a text file containing
>
> # #!/usr/bin/python
> # 
> # html = """
> #     <HTML>
> #             <p>some content</p>
> #             <p> some more content </p>
> #         </HTML>"""
> # 
> # print html
> 
> #the /t, among other escapes, break the html
> v1 <- paste(system("./f.py",intern=TRUE),collapse="")
> v1
[1] "\t\t<HTML>\t\t\t<p>some content</p>\t\t  \t<p> some more content </p>\t\t</HTML>"
> 
> #this is what I want... but it needs to be saved into an object
> system("./f.py")

        <HTML>
            <p>some content</p>
            <p> some more content </p>
        </HTML>
> #or equivalently
> cat(v1)
        <HTML>          <p>some content</p>         <p> some more content </p>      </HTML>
> 
> #I thought capture.output() would work, but the string still has the escaped characters
> v2 <- capture.output(cat(v1))
> v2
[1] "\t\t<HTML>\t\t\t<p>some content</p>\t\t  \t<p> some more content </p>\t\t</HTML>"
4

1 回答 1

1

您的代码运行良好,R 只是将您的转义字符打印为转义。如果你这样做

cat(paste(system("./f.py", intern=TRUE), collapse=""))

你应该看到你想要的输出。

于 2013-06-06T21:32:08.587 回答