1

I have an output that contains a lot of lines and I have a problem that it reads only the first and last lines. I also tried to save them to a text file and it was the same result Can anyone advise me how to fix this?

This is my output:

    -12 -23 0
    -13 -24 0
    -14 -25 0
    ...
    -119 -130 0
    -120 -131 0
    -121 -132 0  

This code gives the data:

    with open('emotion_file.txt', mode='w',newline='\n') as emotion_file:
        emotion_file.write(osem)
        emotion_file.close()

the code retrieves numbers from demofile.txt. It transforms data into fields and works with them

full code:

import os
import sys
import numpy as np
import re
import csv
#read data
f = open("demofile.txt", "r")
lines = f.readlines()
p=1
#sys.stdout = open("results.txt", "w")
 #preprocessing
for i in list(lines):
     if i[0] != '<' and i[0] != '>' and i[0] != '=':
        p = str(' '.join(i.split()))
        print(p)
        
     else:
        #w = i[2:]
        w = i.replace("=",'')
        w = w.replace(">",'')
        w = w.replace("<",'')
        #print(w)
        w = ', '.join(w.split())
        y = i[2]
        y=int(y)+1
        c=np.array([w])
        
        c1 = [int(i) for i in c[0].replace(" ", "").split(",")]
          #transform to array
        c1=np.array(c1)
        frst=c1[0]
        c1=np.delete(c1, 0)       

        n=len(c1)
        sest2=c1
        c1=np.array([c1]*frst)
        c1=np.transpose(c1)   
        left1 = np.array([[(p + j) * 11 for j in range(frst)]] * n) + c1
        left2 = np.array([[(p + j) * -11 for j in range(frst)]] * n) + c1
            
        left=np.where(c1 > 0,left1 , left2)
        #print(left)
    
        left=left*-1
        b=np.all(left < 0) 

                #6.vzorec
        
        sest1=left
        sest = np.zeros((sest1.flatten().shape[0],2)) 
        sest[:,[0]] = sest1.T.flatten()[:,None]
        sest[:,[1]] = np.tile(sest2,frst)[:,None]
        sest=str(sest).replace("[",'')
        sest=str(sest).replace("]",' 0')
        sest = sest[:-1]
        sest=str(sest).replace(".",'')
        sest=str(sest).replace("\n ",'\n')
        sest=str(sest).replace("  ",' ')
        if b==False:
            sest=str(sest).replace("  ",' ')
        else:
           sest=str(sest).replace("  ",' ')
        sest=str(sest).replace("\n ",'\n')
        sest=str(sest).lstrip()

            #7.vzorec
        sedem=np.transpose(left)*-1
        sedem=str(sedem).replace("[",'')
        sedem=str(sedem).replace("]",' 0')
        sedem=str(sedem).replace(".",'')
        sedem = sedem[:-1]
        sedem=str(sedem).replace("\n ",'\n')
        sedem=str(sedem).replace("  ",' ')
        sedem=str(sedem).replace("\n ",'\n')
        sedem=str(sedem).lstrip()
        
        
               
         #8.vzorec       
        if frst==1:
            osem='---nic-----'
        else:    
            osem=left    
            osem = np.vstack([ np.c_[left[:,x], left[:,y]]
            for x, y in np.c_[np.triu_indices(n=left.shape[1], k=1)] ])
            osem=str(osem).replace("[",'')
            osem=str(osem).replace("]",' 0')
            osem = osem[:-1]
            osem=str(osem).replace("\n ",'\n')
            osem=str(osem).replace("  ",' ')
            osem=str(osem).replace("\n ",'\n')
            osem=str(osem).lstrip()
            sedem=str(sedem).replace("  ",'')
            #for iin osem range
  
                
        p +=frst
              
        #print(sest)
        #print(sedem,)
        print(osem,'\n')                

with open('emotion_file.txt', mode='w',newline='\n') as emotion_file:
    emotion_file.write(osem)
    emotion_file.close()

variables sest, sedem and osem transform the fields into different forms

demofile.txt

=>11 1 2 3 4 5 6 7 8 9 10 11 

1 Answers1

0

Depending on what kind of data structure osem is, as that could affect the implementation of your for loop, I found that this worked for me:

osem = [3,4,5]
emotion_file = open('emotion_file.txt', 'a')
for i in osem:
    emotion_file.write(f'{i}\n')
emotion_file.close()

if osem is a dictionary then the following works as well:

osem = {'key_1': 1, 'key_2': 2}
emotion_file = open('emotion_file.txt', 'a')
for key,value in osem.items():
    emotion_file.write(f'{key}\t{value}\n')
emotion_file.close()

If you wish to delete the current data in the emotion_file.txt file, replace 'a' with a 'w' in the open(...) command.

Hopefully this helps!