Reputation: 59
Background is that I have several hundred Yaml files that are configuration files to a system. I want to analyse them as a whole looking for issues in their config.
So I need to pre-process each files just like this this question to remove some illegal characters prior to doing some stuff with them. However I am a python newbie and can not the generators solution proposed to work. The real files should not be modified if possible. I am using python 3.4.
My main problem is that the iterator function does not get called which makes me think I am not accessing the iterator behaviour correctly.
Essentially I have a file processing function that processes all of the files in a directory as follows:
with open(os.path.join(myDir, fileName), 'r') as inputFile:
print("about to call replace_iter")
iterable = replace_iter(inputFile, "push", "force")
print("about to call yaml.safe_load()")
dataMap = yaml.safe_load(iterable)
print("about to process my data map")
type = dataMap['type']
# process the dataMap ites ...
print("done")
And I have a function defined at the top of my file as follows
def replace_iter(iterable, search, replace):
print("replace_iter called")
for value in iterable:
yield value.replace(search, replace)
This gives an output like this skipping the call to the generator function so it suggests that I am not accessing the iterator functionality correctly.
about to call replace_iter
about to call yaml.safe_load()
Upvotes: 0
Views: 350
Reputation: 24133
str.replace
doesn't modify the string, but returns the modified value:
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
You need to yield the return value instead.
def replace_iter(iterable, search, replace):
for value in iterable:
yield value.replace(search, replace)
Upvotes: 2