0

I want to write the output on multiple columns of the file in python. My code generate the output in two lines. code is

f2 = open("C:/Python26/Semantics.txt",'w')
sem = ["cells", "gene","factor","alpha", "receptor", "t","promoter"]
with open("C:/Python26/trigram.txt") as f :
for x in f:
    x = x.strip().split("$")
    f2.write(" ".join(x) + " " + str(len(set(sem) & set(x)))+"\n")
f2.close()

my file looks like this:

IL-2$gene$expression$and
IL-2$gene$expression$and$NF-kappa
IL-2$gene$expression$and$NF-kappa$B
IL-2$gene$expression$and$NF-kappa$B$activation
gene$expression$and$NF-kappa$B$activation$through
expression$and$NF-kappa$B$activation$through$CD28

My current output

IL-2 gene expression and    1
IL-2 gene expression and NF-kappa   1
IL-2 gene expression and NF-kappa B   1
IL-2 gene expression and NF-kappa B activation   1
gene expression and NF-kappa B activation through   1
expression and NF-kappa B activation through CD28   0

My desired output

Token                                            cells   gene    factor……. promoter   
IL-2 gene expression and                          0       1       0     ………       0 
IL-2 gene expression and NF-kappa                 0       1       0     ………       0
IL-2 gene expression and NF-kappa B               0       1       0     ………       0
IL-2 gene expression and NF-kappa B activation    0       1       0     ………       0
gene expression and NF-kappa B activation through 0       1       0     ………       0  
expression and NF-kappa B activation through CD28 0       0       0     ………       0

i think there will required a little bit change in code I think so that it will be solved by nested loop. but how, i dont know. My code for doing so is below which not working

  sem = ["cells", "b","expression", "cell", "gene","factor","activation","protein","activity","transcription","alpha","receptor","t","promotor","mrna","site","kinase","nfkappa","human"];
  f2 = open("C:/Python26/Semantics.txt",'w')
  with open("C:/Python26/trigram.txt") as file :
  for s in sem:
      for lines in file:
          lines = lines.strip().split("$")
          if s==lines:
              f2.write(" ".join(lines) + "\t" +str(len(set(sem) & set(lines)))+"\n")
        f2.write("\n")
   f2.close()   
Shaheen Gul
  • 51
  • 1
  • 4
  • 10

1 Answers1

0

pandas.DataFrame:

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects.

You can create your DataFrame object, then convert it to a string and write() that string to your file.

import pandas

col_labels = ['Token', 'cells', 'gene']
row_labels = ['x', 'y', 'z']

values_array = [[1, 2, 3],
                [10, 20, 30],
                [100, 200, 300]]

df = pandas.DataFrame(values_array, col_labels, row_labels)    
print(df)

Output:

         x    y    z
Token    1    2    3
cells   10   20   30
gene   100  200  300

To save it, convert the object to a string first:

db_as_str = df.to_string()

with open('my_text_file.txt', 'w') as f:
    f.write(db_as_str)

or save it as is, in csv:

db.to_csv('my_text_file.txt')
user
  • 4,434
  • 7
  • 38
  • 66