How to store information in a Python (write) file?

Asked

Viewed 94 times

1

Hello, I’m writing a program in Python with a code I have in Fortran. I have a problem writing this excerpt (from Fortran):

do i=1,n
write(3,350)r(i),(ener(j)*alfa,j=1,6) !meV

end do

I tried to write this way in Python:

file=open("energia_nll2_R50A.dat","w")
for i in range(1,n+1):
  for j in range(1,6):
    file.write("r[i],(ener[j]*alfa)")
file.close()

I was wondering if there’s another equivalent way to write this excerpt in Python.

Complete code:

alfa = 3014.4
r = []
for i in range(1,n1+1):
  x = r1 + (i - 1)*hr1
  r.append(x)
ener = []
for i in range(1,n):
  for j in range(i+1,n+1):
    if ener[i]>ener[j]:
      az = ener[j]
      ener[j] = ener[i]
      ener[i] = az
      for k in range(1,n+1):
        zz3 = vetor[k,j]
        vetor[k,j] = vetor[k,i]
        vetor[k,i] = zz3

file=open("energia_nll2_R50A.dat","w")
for i in range(1,n+1):
  for j in range(1,6):
    file.write("r[i],(ener[j]*alfa)")
file.close()
  • Do you want i and j values to appear in the archive? What are r, ener, alfa and n?

  • i and j here refer to the number of vectors to appear in the file.

  • alpha is a number, n is the precision and r and ener are the vectors

  • I also wanted to know if you are right this way of referencing vectors within write... because I found nothing about.

  • And are they set in Python? Please enter the full code.

  • Ready, I edited the post.

  • Okay, I review my words. Your code made no sense, so explain in words what you intend to do. Describe what your input is, what calculations you want to make, and what the expected output is.

  • Just a question, are you making a header here? file.write("r[i],(ener[j]*alpha)") ? Or is trying to write the values of r[i] and ener[j]*alpha in the file ?

  • I am trying to write them in the archive. For the 6 energy levels (that is this "ener"). It is the variation of the radius "r" to the 6 energy levels "ener". In the Fortran file I opened in Qtiplot, there were 6 energy levels, one in each column - repeating the same number for a given level, n times, in the lines. I need it to plot the energy graph.

Show 4 more comments

1 answer

0

Hi, yes it is possible to write. Let’s go by part.

file=open("energia_nll2_R50A.dat","w")
for i in range(1,n+1):
    for j in range(1,6):
        file.write("r[i],(ener[j]*alfa)")
file.close()

First let’s change the "w" for "w+", so while writing to the archive, the file data will not be overwritten.

Then we’ll build a variable just to make it more readable.

conteudo = "%.f,%.f" %(r[i],ener[j]*alfa) # Caso os parentesis seja necessario, pode adicionar no segundo %.f

Then the piece of code would look like this.

file=open("energia_nll2_R50A.dat","w")
for i in range(1,n+1):
    for j in range(1,6):
        conteudo = "%.f,%.f" %(r[i],ener[j]*alfa)
        file.write(conteudo)
file.close()

Now about the complete code, as I do not know all variables should look like this:

### Variáveis com alguns valores para poder testar.

n1 = 33
r1 = 2.321
hr1 = 0.456
n = 33

##################

alfa = 3014.4
r = []
for i in range(1,n1+1):
  x = r1 + (i - 1)*hr1
  r.append(x)

ener = [] # Como esta variável está vazia, acho que você insere alguma coisa nela, porém como não está escrito, assume-se uma lista com valores aleatórios.


### Como não vi a variável vetor, então criei uma, do tipo dicionário.
### Pois ela aceita um índice do tipo tupla. " (k,i) " or " k,i "

vetor={} # Porém não sei os valores que você guarda nela. Se você adicionar o mais informações eu completo a resposta.

##################

for i in range(1,n):
  for j in range(i+1,n+1):
    if ener[i]>ener[j]:
      az = ener[j]
      ener[j] = ener[i]
      ener[i] = az
      for k in range(1,n+1):
        zz3 = vetor[k,j]
        vetor[k,j] = vetor[k,i]
        vetor[k,i] = zz3

file=open("energia_nll2_R50A.dat","w+")
for i in range(1,n+1):
    for j in range(1,6):
        conteudo = "%.f,%.f" %(r[i],ener[j]*alfa)
        file.write(conteudo)
file.close()

So it should already work. Anything just ask.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.