Home > Net >  Print strings from a sublist with no quotes and in new lines (no commas)
Print strings from a sublist with no quotes and in new lines (no commas)

Time:05-27

Context: I'm trying to print a sub list of genes without commas, in new lines and without quotes.

Currently, I have this: (maybe the fact that the output is with single quotes and then double quotes is the issue)

gene_list = []

for f in files:
    temp_gene_list = []
    with open('{}\{}'.format(path,f),'r') as fi:
        for line in fi:
            fields = line.strip().split(' ')
            if fields[-1] not in temp_gene_list:
                temp_gene_list.append(fields[-1])
        gene_list.append(temp_gene_list)
    
print ("Test", ("{0}".format(', '.join(map(str, gene_list)))),sep='\n',)

for lst in gene_list:
    print("###########START##################")
    for item in lst:
        print(item, sep='\n')
    print("############END#################")

But I keep getting this output:

enter image description here

I'm using VS code and I'd like the print output to be exactly that one, without quotes. I've tried using join, map and a bunch of other combinations, but no success

Thank you in advance.

The gene_list is like this: (maybe the fact that its in single quotes and double quotes is giving this issue)

[['"geneName"','"STEAP2"', '"ADGRF4"', '"SNED1"', '"PF4V1"', '"CEACAM1"', '"MGAT4A"', '"ZNF536"', '"BTBD11"', '"CD40"', '"FAM83A-AS1"', '"CSF3R"', '"TBXA2R"', '"SLC35F3"', '"LHFPL2"', '"AFF2"', '"PDK2"'], ['"geneName"', '"CDKN1A"', '"LGALS9C"', '"SPATA18"', '"FGL1"', '"TF"', '"ORM1"', '"PHLDA3"', '"CHRM5"', '"FMOD"', '"AC093866.1"', '"DKK3"', '"GAS6-AS1"', '"SDC2"', '"PPARGC1A"', '"EPS8L2"', '"PARM1"', '"SCNN1B"', '"CHRD"', '"BTG2"']]

CodePudding user response:

The strings in your list has quotes. You can use strip.('"') to get rid of the surrounding quotes in your string:

gene_list = [['"geneName"','"STEAP2"', '"ADGRF4"', '"SNED1"', '"PF4V1"', '"CEACAM1"', '"MGAT4A"', '"ZNF536"', '"BTBD11"', '"CD40"', '"FAM83A-AS1"', '"CSF3R"', '"TBXA2R"', '"SLC35F3"', '"LHFPL2"', '"AFF2"', '"PDK2"'], ['"geneName"', '"CDKN1A"', '"LGALS9C"', '"SPATA18"', '"FGL1"', '"TF"', '"ORM1"', '"PHLDA3"', '"CHRM5"', '"FMOD"', '"AC093866.1"', '"DKK3"', '"GAS6-AS1"', '"SDC2"', '"PPARGC1A"', '"EPS8L2"', '"PARM1"', '"SCNN1B"', '"CHRD"', '"BTG2"']]

for lst in gene_list:
    print("###########START##################")
    print(*(item.strip('"') for item in lst), sep="\n")
    print("############END#################")

Output

###########START##################
geneName
STEAP2
ADGRF4
SNED1
PF4V1
CEACAM1
MGAT4A
ZNF536
BTBD11
CD40
FAM83A-AS1
CSF3R
TBXA2R
SLC35F3
LHFPL2
AFF2
PDK2
############END#################
###########START##################
geneName
CDKN1A
LGALS9C
SPATA18
FGL1
TF
ORM1
PHLDA3
CHRM5
FMOD
AC093866.1
DKK3
GAS6-AS1
SDC2
PPARGC1A
EPS8L2
PARM1
SCNN1B
CHRD
BTG2
############END#################
  • Related