【发布时间】:2022-01-03 12:22:46
【问题描述】:
假设我有一个文件:
This is the first line
Ages = ["young*","old*"] //This was the second line, I put a * on purpose
This is the third line
场景如下:
- 我知道文件中有“Ages”数组,但我不知道它的元素。
- 我现在想在每个元素后附加一个特定的字符串,比如“test*”,文件将变为:
This is the first line
Ages = ["young\*test\*","old\*test\*"] //This was the second line, I put a * on purpose
This is the third line
有什么帮助吗?
【问题讨论】:
-
1) 读取文件。 2)找到那条线。 3) 使用
ast.literal_eval或json.loads解析列表文字。 4) 修改列表。 5)把它重新组合成一个字符串。 6) 将其全部写回文件中。
标签: python arrays parsing split element