[英]Make a file more readable
Say we have a file with the following inside:假设我们有一个包含以下内容的文件:
{{{banana},{kinky},{bronze-spaghetti,
definitly-not},{legitimate-style, witness,
drunk}}}
I wanted to rearrange this file so that it look like this:我想重新排列这个文件,使它看起来像这样:
{{{banana},
{kinky},
{bronze-spaghetti,definitly-not},
{legitimate-style, witness, drunk}}}
Notice that each line ends with },
except for the last one.请注意
},
除了最后一行之外,每一行都以},
结尾。 Now my question is how would I do this?现在我的问题是我将如何做到这一点?
you can find the regex "}," and insert after it a new line你可以找到正则表达式“}”,然后在它后面插入一个新行
edit: as jDo suggested, you can do something like that:编辑:正如 jDo 建议的那样,您可以执行以下操作:
with open('in.txt', 'r') as infile:
data=infile.read()
with open('out.txt', 'w') as outfile:
outfile.write(data.replace("\n", "").replace("},", "},\n"))
I think you could simply use replace
(triple quotes for multi-line strings):我认为您可以简单地使用
replace
(多行字符串的三重引号):
original_text = """{{{banana},{kinky},{bronze-spaghetti, definitly-not},{legitimate-style, witness, drunk}}}"""
reformatted = original_text.replace("\n", "").replace("},", "},\n")
print(reformatted)
This can easily be done in the shell:这可以在 shell 中轻松完成:
>>> my_str = """{{{banana},{kinky},{bronze-spaghetti,
... definitly-not},{legitimate-style, witness,
... drunk}}}"""
>>>
>>> my_str = my_str.replace("\n", "").replace("},", "},\n")
>>>
>>> print(my_str)
{{{banana},
{kinky},
{bronze-spaghetti,definitly-not},
{legitimate-style, witness,drunk}}}
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.