【问题标题】:Read to right of specific symbol in file from Python从 Python 读取文件中特定符号的右侧
【发布时间】:2011-04-27 00:15:20
【问题描述】:

我将分离 1000 多个病毒签名和病毒名称。我将它们全部放在一个文本文件中,并想用 python 来做到这一点。

格式如下:

virus=signature

我需要能够获取“病毒”并将其写入一个文件,然后获取“签名”并将其写入另一个文件。


这是我到目前为止所绑定的

h = open("FILEWITHSIGS")
j = h.read()
k = h.split('=')

这基本上就是我卡住的地方。无论我尝试什么,它都打印(或写入)到同一个地方。

【问题讨论】:

  • 您只打开了一个文件并希望写入两个位置?这是什么逻辑?

标签: python file string


【解决方案1】:
f1=open("first.txt","a")
f2=open("second.txt","a")
for line in open("file"):
    s=line.split("=",1)
    f1.write(s[0]+"\n")
    f2.write(s[-1])
f1.close()
f2.close()

【讨论】:

  • 如果签名包含一个'='..怎么办?抱歉,SilentGhost 正在获得我的 1UP。
【解决方案2】:
with open(fname) as inputf, open(virf, 'w') as viruses, open(sigs, 'w') as signatures:
    for line in inputf:
        virus, _, sig = line.partition('=')
        viruses.write(virus + '\n')
        signatures.write(sig)

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2013-10-26
    • 2016-04-11
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多