python中的文件读取
所以我的整个问题是我有两个文件,其中一个具有以下格式(对于Python 2.6):
#comments
config = {
#comments
'name': 'hello',
'see?': 'world':'ABC',CLASS=3
}
该文件有很多这样的部分。第二个文件具有格式:
[23]
[config]
'name'='abc'
'see?'=
[23]
现在的要求是我需要比较两个文件并生成文件为:
#comments
config = {
#comments
'name': 'abc',
'see?': 'world':'ABC',CLASS=3
}
因此结果文件将包含第一个文件中的值,除非第二个文件中存在相同属性的值,这将覆盖价值。现在我的问题是如何使用Python 操作这些文件。
提前感谢您之前在短时间内的回答,我需要使用 python 2.6
So my whole problem is that I have two files one with following format(for Python 2.6):
#comments
config = {
#comments
'name': 'hello',
'see?': 'world':'ABC',CLASS=3
}
This file has number of sections like this. Second file has format:
[23]
[config]
'name'='abc'
'see?'=
[23]
Now the requirement is that I need to compare both files and generate file as:
#comments
config = {
#comments
'name': 'abc',
'see?': 'world':'ABC',CLASS=3
}
So the result file will contain the values from the first file, unless the value for same attribute is there in second file, which will overwrite the value. Now my problem is how to manipulate these files using Python.
Thanks in advance and for your previous answers in short time ,I need to use python 2.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于评论而无法找到一个漂亮的解决方案。这已经过测试并且适用于我,但需要 Python 3.1 或更高版本:
输出:
Was unable to find a beautiful solution due to the comments. This is tested and works for me, but requires Python 3.1 or higher:
Output:
我花了很长一段时间才写出下面的代码。
我很难用逗号来管理。我希望更新后的文件在更新后具有与更新 : 行之前更新的文件相同的格式,以逗号结尾,最后一行除外。
该代码是针对提问者提出的特定问题而设计的,不能按原样用于其他类型的问题。我知道。这是使用基于正则表达式而不是解析器的代码的问题,我完全意识到这一点。但我认为,通过更改正则表达式,它是一个可以相对容易地适应其他情况的画布,由于正则表达式的可塑性,这是一个相对容易的过程。
示例代码:
结果:
.
编辑:
我改进了代码,因为我仍然不满意。现在,“变量”front 捕获包含数据的行开头的空白字符(
' '
或'\t'
)要更新的文件。我还忘记了指令
f.truncate()
,该指令对于不保留不需要的字符的尾部非常重要。我很高兴看到我的代码即使在以下文件中也能正常工作,其中值是字典,如 Jagdev 所示:
这证实了我选择逐行处理,而不是尝试使用一个正则表达式。
。
编辑2:
我再次更改了代码。更新是由一个函数执行的,该函数接受以下参数:
更新文件的名称(包含用于更新另一个文件的数据的文件)
以及适合从该特定更新文件中提取数据的函数
因此,可以更新给定文件包含来自各种更新文件的数据。这使得代码更加通用。
I had a really long and hard time to manage to write the following code.
I had difficulties to manage with commas. I wanted the updated file to have after the updating the same format as the file to update before the updating : lines end with a comma, except for the last one.
This code is crafted for the particular problem as exposed by the questioner and can't be used as-is for another type of problem. I know. It's the problem of using a code based on regex and not on a parser, I'm fully aware of that. But I think that it is a canvas that can be relatively easily adapted to other cases, by changing the regexes, which is a relatively readily process thanks to the malleability of regexes.
Exemplifying code:
Result:
.
EDIT:
I have improved the code because I was still insatisfied. Now the "variable" front catches the blank characters (
' '
or'\t'
) at the beginning of the data-containing lines in the file to be updated.I had also forgot the instruction
f.truncate()
which is very important to not keep a tail of undesired characters.I am satisfied to see that my code works well even with the following file in which a value is a dictionnary, as presented by Jagdev:
That confirms me in my choice to process line after line , and not trying to run through the entire file with a regex.
.
EDIT 2:
I again changed the code. The updating is performed by a function that takes as arguments :
the name of the updating file (the file containing the data used to udpdate another file)
and the function that is suited to extract the data from this particular updating file
Hence, it is possible to update a given file with data from various updating files. That makes the code more generic.
非常粗略(即这根本没有经过测试,并且可以进行许多改进,例如使用正则表达式和/或漂亮打印):
Very roughly (i.e. this hasn't been tested at all, and there are numerous imprvements that could be made such as the use of regex and/or pretty-printing):