将字符串视为python中的文件 [英] Treat a string as a file in python

查看:82
本文介绍了将字符串视为python中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了 not 重写开源库,我想将一串文本作为python 3中的文件处理.

In the interest of not rewriting an open source library, I want to treat a string of text as a file in python 3.

假设我将文件内容作为字符串:

Suppose I have the file contents as a string:

not_a_file = 'there is a lot of blah blah in this so-called file'

我想将此变量(即文件的内容)视为功能.

I want to treat this variable, i.e. the contents of a file, as a path-like object that way I can use it in python's open() function.

这是一个显示我的困境的简单示例:

Here is a simple example which shows my dilemma:

not_a_file = 'there is a lot of blah blah in this so-called file'
file_ptr = open(not_a_file, 'r')

很明显,该示例不起作用,因为not_a_file不是类似路径的对象.我不想写文件,也不想创建任何临时目录以方便携带.

Clearly the example doesn't work because not_a_file is not a path-like object. I don't want to write a file nor create any temporary directories for portability purposes.

话虽如此,我需要解决这个谜团:

With that said, I need is to solve this mystery:

not_a_file = 'there is a lot of blah blah in this so-called file'
... Something goes here ... 
file_ptr = open(also_not_a_file, 'r') 

到目前为止我尝试过的事情

  1. 我研究了StringIO,并尝试将其用作类似路径的对象并且没有骰子: import StringIO output = StringIO.StringIO() output.write('First line.\n') file_ptr = open(output,'r') 嗯,这是行不通的,因为StringIO不是路径类对象.

  1. I've looked into StringIO and tried using that as a path-like object and no dice: import StringIO output = StringIO.StringIO() output.write('First line.\n') file_ptr = open(output,'r') Well this doesn't work because StringIO isn't a path-like object.

我以类似的方式尝试了tempfile,但没有成功. import tempfile tp = tempfile.TemporaryFile() tp.write(b'there is a lot of blah blah in this so-called file') open(tp,'r')

I've tried tempfile in a similar fashion with no success. import tempfile tp = tempfile.TemporaryFile() tp.write(b'there is a lot of blah blah in this so-called file') open(tp,'r')

感谢您的帮助! :-)

Any help is appreciated! :-)

如果 PurePath 初始化为文件.也许我可以创建一个继承PurePath的类的实例,当被open()读取时,它会读取我的字符串.让我举个例子:

So pathlib.PurePath can work with open() if PurePath is initialized to a file. Perhaps I can create an instance of a class that inherits PurePath and when read by open(), it reads my string. Let me give an example:

from pathlib import PurePath

not_a_file = 'there is a lot of blah blah in this so-called file'
class Magic(PurePath):
    def __init__(self, string_input):
        self.file_content = string_input
        PurePath.__init__(self, 'Something magical goes here')
    #some more magic happens in this class

also_not_a_file = Magic(not_a_file)
fp = open(also_not_a_file,'r')
print(fp.readlines()) # 'there is a lot of blah blah in this so-called file'

推荐答案

您可以创建一个临时文件并传递其名称以打开:

You can create a temporary file and pass its name to open:

在Unix上:

tp = tempfile.NamedTemporaryFile()
tp.write(b'there is a lot of blah blah blah in this so-called file')
tp.flush()
open(tp.name, 'r')

在Windows上,您需要先关闭临时文件,然后才能打开它:

On Windows, you need to close the temporary file before it can be opened:

tp = tempfile.NamedTemporaryFile(delete=False)
tp.write(b'there is a lot of blah blah blah in this so-called file')
tp.close()
open(tp.name, 'r')

使用完文件后,您将负责删除该文件.

You then become responsible for deleting the file once you're done using it.

这篇关于将字符串视为python中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆