繁体   English   中英

Python - 从大型 .csv 文件中的文本文件中搜索字符串列表

[英]Python - Search a list of strings from a text file in a large .csv file

抱歉,我可能在这里犯了一大堆错误,但我正在尝试使用文件 (justgenes.txt) 中的字符串列表搜索大型 CSV 文件,并返回包含 justgenes 列表中的字符串的行。

我一直在很大程度上使用 BASH,但是我的代码占用了超过 100GB 的内存并且崩溃了:

grep -f justgenes.txt allDandHunique.csv > HPCgenesandbugs.csv

因此,我试图用 python 来做,假设它会更有效率,但我对它知之甚少。

我使用这个代码(我从网上抓取的),但最后得到一个空文件:

data = open('allDandHunique.csv')
                
with open('justgenes.txt', "r+") as file1:
    fileline1= file1.readlines()
    for x in data: # <--- Loop through the list to check      
        for line in fileline1: # <--- Loop through each line
            if x in line:
                 print(x)

justgenes 文件如下所示:

1A0N_B
1A1A_A
1A4I_A
1A5Y_A
1ACO_A
1AGN_A
1AGS_A
1AJE_A
1AJJ_A
1AP0_A
1APQ_A

虽然 csv 看起来像这样:

"0403181A:PDB=1BP2,2BPP",
"0403181A:PDB=1BP2,2BPP",,,
"0706243A:PDB=1HOE,2AIT,3AIT,4AIT",
"0706243A:PDB=1HOE,2AIT,3AIT,4AIT",,,
"1309311A:PDB=1EMD,2CMD",
"1309311A:PDB=1EMD,2CMD",,,
"1513188A:PDB=1BBC,1POD",
"1513188A:PDB=1BBC,1POD",,,
0308206A,
0308206A,,,
0308221A,
0308221A,,,
0308230A,
0308230A,,,

任何帮助将不胜感激。

我会用熊猫来完成这个。

尝试类似:

import pandas as pd

df = pd.read_csv('allDandHunique.csv')

with open('justgenes.txt', "r+") as file1:
    fileline1= file1.readlines()
    for x in fileline1: 
      for col in df:
         if col.str.contains(x, regex=False):
             ##do something here##

如果在读取文件时得到一个空白文件,我会检查并确保路径正确。

由于我没有这些文件,因此我无法自己测试,但我认为此代码可能会有所帮助。

data = open('allDandHunique.csv')
        
for x in data: # <--- Loop through the list to check      
    with open('justgenes.txt', "r+") as file1:
        fileline1= file1.readlines()
        for line in fileline1: # <--- Loop through each line
            if x in line:
                    print(x)

对于x in data每个x in data您必须遍历 file1 中的所有行。 如果我没有错,你需要为每次迭代打开你的文件,否则,当你到达 EOF 时它什么都不返回。

import csv

with open('justGenes') as infile:
    searchTargets = set(line.strip() for line in infile)


with open('allDandHunique.csv') as infile:
    for row in csv.reader(infile):
        if any(target in row for target in searchTargets):
            print(row)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM