How keep the TAB in file generated by yaml.dump

Hi everyone,

while doing

with open('aFile.yml', 'w') as file:
	yaml.dump(DataObj, file, allow_unicode=True, sort_keys=False)

The generated file contain \t for all the tab but in the source of DataObj the tab are “display”
and not escaped.

do you know anyway to keep the tab displayed and not escaped ?

Thanks.

Do you mean that there are strings in dataobj that have tabs in them?
And that the tab is not saved correctly?

Do you have a simple example showing the problem that includes setting up dataobj and saving it?

The DataObj has been generated with a yaml.safe_load(file)
and in the file indeed they were some tab made by pushing the keyboard tab key and display as ~
and those tab become \t once using yaml.dump()

Does it cause a problem? When you yaml.load the file again, you should have “real” tabs in the data.

They’re saved like that to make sure you can see them properly in the output. Tabs don’t have a fixed width, and it’s easy to mistake them some amount of spaces (possibly one).

This is actually happening, not a misunderstanding of \t. I can reproduce that the yaml package does this in yaml.dump:

d = yaml.safe_load("""
aha:
    - "foo\ta"  # contains a tab, prints as expected in python
    - faa
""")
yaml.dump(d)
# 'aha:\n- "foo\\ta"\n- faa\n'  # escapes the \ to print a literal \t

Unfortunately I’m not sure what the fix is, besides perhaps a manual yaml.dump(...).replace("\\t", "\t") to clean it up afterwards. The (apparently very old) documentation for PyYAML says this about tabs:

1 Like

Oh, I see.

Yeah, what I read from the documentation made it sound like they’re only expecting \u and \x escapes in the incoming YAML. None of it seems to be written with much technical detail, either.

Are there other options for this? PyPI search results for yaml are numerous, but don’t look very promising. (I don’t even get PyYAML on the first page.)

I think it’s a combination of “the package has a different name” [1] and PyYAML is a very generic descriptor. I’ve never used or needed anything else for YAML files, and it’s very widely used as a dependency. But I guess it doesn’t like tabs!


  1. package is PyYAML but it’s import yaml ↩︎

@kknechtel

Does it cause a problem? When you yaml.load the file again, you should have “real” tabs in the data

You right when I yaml.load() the tab are indeed process\displayed as it should. but nevertheless tab can be either stored as \t (escaped) or and like the purpose of yaml is to be human readable in my opinion they should not be stored as escaped \t

Thanks all you input :+1:

(I wanted to mention all of your names but I get this error

An error occurred: Sorry, new users can only mention 2 users in a post.

I’m wondering why this limit )

@kknechtel

Does it cause a problem? When you yaml.load the file again, you should have “real” tabs in the data

You right when I yaml.load() the tab are indeed process\displayed as it should. but nevertheless tab can be either stored as \t (escaped) or and like the purpose of yaml is to be human readable in my opinion they should not be stored as escaped \t

As remarked, a TAB rendered as spaces can render differently depending
on the terminal tab settings and the indentation of the text when
printed/displayed. So IMO a \t is far better, as I, a human, would
know I’m looking at a TAB and not some unknown number of spaces, of
maybe a TAB, or maybe some spaces and then a TAB, or …

(I wanted to mention all of your names but I get this error

An error occurred: Sorry, new users can only mention 2 users in a post.
I’m wondering why this limit )

There’s a bunch of limitations for new users, partly to prevent spambots
(imagine something showing up and at-ing a hundred users to attract
their eyeballs). The limits go away with time and with further activity.

2 Likes