如何将一个文本文件拆分为多个文件?
我提取了一个具有以下格式的文本文件:
user1
$ANSIBLE_VAULT;1.1;AES256
33643334383262616539323337343966353463303464626331396638373435376565383963366463
3037383439333665636136653165613631336538313662660a656132393966613839373563636463
31356165663036643535306563393663363462316661613435313133666234656361326433356366
6637623462316363360a343066323363626633383631303032633334626365353734363862663332
3636
user2
$ANSIBLE_VAULT;1.1;AES256
65663365353466313564306261666634316137343438623432353030343832326632346333663332
6232393236626563326630383639636337346465386664320a663363643836663266613366313030
31313836336461303863366338613236323631336264363632316561646663663431306633333036
6562633430656630390a323034616232353430396261303436623037663665363736613530336431
6135
user3
$ANSIBLE_VAULT;1.1;AES256
35346138316166343430643434396566343761666361343935386464343638376532343566356331
6134393664646666626637643035373961613931353334360a626565336466316336313863313466
64383061323966316261656231613538663162666236386136396430663433343134303131326434
3461666634333437350a613766636361393236633262613739653962643732643135363561333534
6332
user4
$ANSIBLE_VAULT;1.1;AES256
62633764663030333566373434303632343030353163363661303066636431383936306264373235
3266316631383930303832643265316630353236323437350a356338316261616137633632333961
62646234363237383838366361383235393638376239386264333361373438616530323338656436
3836373463386663370a356164383538643131373337376638656236633630313433326362333334
6139
我需要按每个换行符拆分文件以接收以用户(1,4)命名且仅包含 ansible 秘密的文件。
我尝试过的方法:
from sys import argv
with open(argv[1], "r") as file:
for line in file:
fname = ""
secret = []
if line.startswith("user"):
fname = line
elif line.startswith("\\$ANSIBLE"):
secret.append(line)
elif line[0].isdigit():
secret.append(line)
elif len(line.strip()) == 0:
f = open(fname, "x")
for i in secret:
f.write(i)
f.close()
else:
print("This is not supposed to happen.)"
到目前为止,我遇到了 SyntaxError: Unexpected EOF while parsing
,感觉这是最低效的方法。
I extracted a text file with the following format:
user1
$ANSIBLE_VAULT;1.1;AES256
33643334383262616539323337343966353463303464626331396638373435376565383963366463
3037383439333665636136653165613631336538313662660a656132393966613839373563636463
31356165663036643535306563393663363462316661613435313133666234656361326433356366
6637623462316363360a343066323363626633383631303032633334626365353734363862663332
3636
user2
$ANSIBLE_VAULT;1.1;AES256
65663365353466313564306261666634316137343438623432353030343832326632346333663332
6232393236626563326630383639636337346465386664320a663363643836663266613366313030
31313836336461303863366338613236323631336264363632316561646663663431306633333036
6562633430656630390a323034616232353430396261303436623037663665363736613530336431
6135
user3
$ANSIBLE_VAULT;1.1;AES256
35346138316166343430643434396566343761666361343935386464343638376532343566356331
6134393664646666626637643035373961613931353334360a626565336466316336313863313466
64383061323966316261656231613538663162666236386136396430663433343134303131326434
3461666634333437350a613766636361393236633262613739653962643732643135363561333534
6332
user4
$ANSIBLE_VAULT;1.1;AES256
62633764663030333566373434303632343030353163363661303066636431383936306264373235
3266316631383930303832643265316630353236323437350a356338316261616137633632333961
62646234363237383838366361383235393638376239386264333361373438616530323338656436
3836373463386663370a356164383538643131373337376638656236633630313433326362333334
6139
I need to split the file by each newline to receive files which are named after the user(1,4) and only contain the ansible secret.
What I tried:
from sys import argv
with open(argv[1], "r") as file:
for line in file:
fname = ""
secret = []
if line.startswith("user"):
fname = line
elif line.startswith("\\$ANSIBLE"):
secret.append(line)
elif line[0].isdigit():
secret.append(line)
elif len(line.strip()) == 0:
f = open(fname, "x")
for i in secret:
f.write(i)
f.close()
else:
print("This is not supposed to happen.)"
I get a SyntaxError: unexpected EOF while parsing
so far and feel like this is the most inefficient method possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@JANO 找到了它,但发布在评论中。
@JANO found it but posted in comments.