替换流中的某些字符 [英] Replace certain characters in stream
问题描述
我有一个将输入流作为输入的方法(一个 .yml 解析器).问题是它在某些地方遇到某些字符时会抛出错误,例如%
.
I have a method (a .yml parser) that takes an input stream as input. The problem is that it throws errors when it encounters certain characters in certain places e.g. %
.
我想做的是获取流,用占位符替换所有 %
,然后将其传递给解析器.
What I would like to do is take the stream, replace all of the %
with a place holder, and then pass it to the parser.
这是我所拥有的(不适用于当前输入):
This is what I have (which doesn't work with the current input):
stream = open('file.yml', 'r')
dict = yaml.safe_load(stream)
但我认为我需要的是:
stream = open('file.yml', 'r')
temp_string = stringFromString(stream) #convert stream to string
temp_string.replace('%', '_PLACEHOLDER_') #replace with place holder
stream = streamFromString(temp_String) #conver back to stream
dict = yaml.safe_load(stream)
推荐答案
显然这里的原始答案不再有效,并且该库现在需要一个类似文件的对象.
Apparently the original answer here no longer appears to work, and the library now requires a file-like object.
鉴于此,它变得有点尴尬.您可以编写自己的包装器,以类似文件的方式起作用(其基础可能是 io.TextIOBase
) 并在缓冲区中进行替换,但如果您愿意牺牲懒惰,最简单的解决方案大致是最初的问题中建议:在内存中进行替换.
Given that, it becomes a little more awkward. You could write your own wrapper that acts in a file-like way (the basis for this would probably be io.TextIOBase
) and does the replacement in a buffer, but if you are willing to sacrifice laziness, the easiest solution is roughly what was originally suggested in the question: do the replacement in memory.
将字符串转换为类文件对象的解决方案是 io.StringIO
.
The solution for turning a string into a file-like object is io.StringIO
.
旧答案:
这样做的一个好方法是编写一个生成器,这样它就可以保持惰性(不需要一次读入整个文件):
A good way of doing this would be to write a generator, that way it remains lazy (the whole file doesn't need to be read in at once):
def replace_iter(iterable, search, replace):
for value in iterable:
value.replace(search, replace)
yield value
with open("file.yml", "r") as file:
iterable = replace_iter(file, "%", "_PLACEHOLDER")
dictionary = yaml.safe_load(iterable)
注意使用 with
语句打开文件 - 这是在 Python 中打开文件的最佳方式,因为它确保文件被正确关闭,即使发生异常.
Note the use of the with
statement to open the file - this is the best way to open files in Python, as it ensures files get closed properly, even when exceptions occur.
另请注意,dict
是一个糟糕的变量名,因为它会破坏内置的 dict()
并阻止您使用它.
Also note that dict
is a poor variable name, as it will smash the built in dict()
and stop you from using it.
请注意,您的 stringFromStream()
函数本质上是 file.read()
,而 steamFromString()
是 data.splitlines()
.您所说的流"实际上只是字符串(文件行)上的迭代器.
Do note that your stringFromStream()
function is essentially file.read()
, and steamFromString()
is data.splitlines()
. What you are calling a 'stream' is actually just an iterator over strings (the lines of the file).
这篇关于替换流中的某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!