打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何在python中逐行并行读取两个文件?

我一直在努力解决这一问题,但没有成功.

我有一个“原始文件”,我们称它为“ infile”,这是我要编辑的文件.
另外,我还有另一个充当“字典”的文件,我们称它为“ inlist”.

以下是infile的示例:

PRMT6   10505   Q96LA8  HMGA1   02829   NP_665906WDR77   14387   NP_077007   SNRPE   00548   NP_003085NCOA3   03570   NP_858045   RELA    01241   NP_068810ITCH    07565   Q96J02  DTX1    03991   NP_004407

和inlist:

NP_060607   Q96LA8NP_001244066    Q96J02NP_077007   Q9BQA1NP_858045   Q9Y6Q9

我当前的方法是在相应的列中拆分行,并通过现有的选项卡拆分行.
目的是读取infile的每一行并检查一些内容:

>如果在inlist的第一列中找到了infile的第三列中的元素,则将该元素更改为inlist的第二列中的相应元素
>如果在inlist的第二列中找到了infile的第三列中的元素,则不执行任何操作
> infile的第5列相同

这应该检索输出:

PRMT6   10505   Q96LA8  HMGA1   02829   Q(...)WDR77   14387   Q9BQA1  SNRPE   00548   Q(...)NCOA3   03570   Q9Y6Q9  RELA    01241   Q(...)ITCH    07565   Q96J02  DTX1    03991   Q(...)

注意:并非所有代码都以Q开头

我已经尝试过使用while循环,但是没有成功,我很ham愧在这里发布代码(我是编程新手,所以我不想在游戏中这么早就灰心了) .
解决此问题的最佳方法是:

for line in inlist #, infile: <--- THIS PART! Reading both files, splitting both files, replacing both files...        inlistcolumns = line.split('\t')        infilecolumns = line.split('\t')        if inlistcolumns[0] in infilecolumns[2]:            outfile.write(str(infilecolumns[0])   "\t"   str(infilecolumns[1])   "\t"   str(inlistcolumns[1])   "\t"   str(infilecolumns[3])   "\t"   str(infilecolumns[4])   "\t"   str(infilecolumns[5])   "\n")        elif inlistcolumns[0] in infilecolumns[5]:            outfile.write(str(infilecolumns[0])   "\t"   str(infilecolumns[1])   "\t"   str(infilecolumns[2])   "\t"   str(infilecolumns[3])   "\t"   str(infilecolumns[4])   "\t"   str(inlistcolumns[1])   "\n")        else:            outfile.write('\t'.join(infilecolumns)   '\n')

帮助将不胜感激.谢谢!

好的,在Sephallia和Jlengrand的提示下,我得到了:

for line in infile:    try:    # Read lines in the dictionary        line2 = inlist.readline()        inlistcolumns = line.split('\t')        infilecolumns = line.split('\t')        if inlistcolumns[0] in infilecolumns[2]:            outfile.write(str(infilecolumns[0])   "\t"   str(infilecolumns[1])   "\t"   str(inlistcolumns[1])   "\t"   str(infilecolumns[3])   "\t"   str(infilecolumns[4])   "\t"   str(infilecolumns[5]))        elif inlistcolumns[0] in infilecolumns[5]:                outfile.write(str(infilecolumns[0])   "\t"   str(infilecolumns[1])   "\t"   str(infilecolumns[2])   "\t"   str(infilecolumns[3])   "\t"   str(infilecolumns[4])   "\t"   str(inlistcolumns[1]))        else:                    outfile.write('\t'.join(infilecolumns))    except IndexError:        print "End of dictionary reached. Restarting from top."

问题在于,显然if语句没有完成其工作,因为输出文件仍然等于输入文件.我做错了什么?

编辑2:

如某些人所问,完整代码如下:

    import osdef replace(infilename, linename, outfilename):    # Open original file and output file    infile = open(infilename, 'rt')    inlist = open(linename, 'rt')    outfile = open(outfilename, 'wt')    # Read lines and find those to be replaced    for line in infile:        infilecolumns = line.split('\t')        line2 = inlist.readline()        inlistcolumns = line2.split('\t')        if inlistcolumns[0] in infilecolumns[2]:            outfile.write(str(infilecolumns[0])   "\t"   str(infilecolumns[1])   "\t"   str(inlistcolumns[1])   "\t"   str(infilecolumns[3])   "\t"   str(infilecolumns[4])   "\t"   str(infilecolumns[5]))        elif inlistcolumns[0] in infilecolumns[5]:            outfile.write(str(infilecolumns[0])   "\t"   str(infilecolumns[1])   "\t"   str(infilecolumns[2])   "\t"   str(infilecolumns[3])   "\t"   str(infilecolumns[4])   "\t"   str(inlistcolumns[1]))        outfile.write('\t'.join(infilecolumns))    # Close files    infile.close()    inlist.close()    outfile.close()if __name__ == '__main__':    wdir = os.getcwd()    outdir = os.path.join(wdir, 'results.txt')    outname = os.path.basename(outdir)    original = raw_input("Type the name of the file to be parsed\n")    inputlist = raw_input("Type the name of the libary to be used\n")    linesdir = os.path.join(wdir, inputlist)    linesname = os.path.basename(linesdir)    indir = os.path.join(wdir, original)    inname = os.path.basename(indir)    replace(indir, linesdir, outdir)    print "Successfully applied changes.\nOriginal: %s\nLibrary: %s\nOutput:%s" % (inname, linesname, outname)

要使用的第一个文件是hprdtotal.txt:https://www.dropbox.com/s/hohvlcdqvziewte/hprdmap.txt
第二个是hprdmap.txt:https://www.dropbox.com/s/9hd0e3a8rt95pao/hprdtotal.txt

希望这可以帮助.

解决方法:

这样的东西行不通吗?

(按照您的代码段)

for line in infile: # read file 1 one line after the other        try            line2 = inlist.readline() # read a line of file 2        catch Exception:            print "End of file 2 reached"        inlistcolumns = line.split('\t')        infilecolumns = line.split('\t')        if inlistcolumns[0] in infilecolumns[2]:            outfile.write(str(infilecolumns[0])   "\t"   str(infilecolumns[1])   "\t"   str(inlistcolumns[1])   "\t"   str(infilecolumns[3])   "\t"   str(infilecolumns[4])   "\t"   str(infilecolumns[5])   "\n")        elif inlistcolumns[0] in infilecolumns[5]:            outfile.write(str(infilecolumns[0])   "\t"   str(infilecolumns[1])   "\t"   str(infilecolumns[2])   "\t"   str(infilecolumns[3])   "\t"   str(infilecolumns[4])   "\t"   str(inlistcolumns[1])   "\n")        else:            outfile.write('\t'.join(infilecolumns)   '\n')

我真的不明白为什么不先将文件保存在内存中,然后再进行简单的模式研究.
我有适当的理由让您同时读取两个文件吗? (文件1的第45行与文件2的第45行匹配吗?)

来源:https://www.icode9.com/content-1-540451.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
130+ 条 Vim 常用命令
VC++实现——断句
C++-加载EXCEL数据
sstream和strstream以及fstream
BOM utf
fputc(c,out)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服