How to save requests return and save to . txt or . csv file in Python

Asked

Viewed 203 times

0

I wonder if it is possible to save the return of a request in file . txt or . csv in Python. I need some NIST data and for that I made the following code:

    import requests
    from bs4 import BeautifulSoup
    import lxml
    arquivo = open('file14.txt', 'w')
    req = requests.get('https://physics.nist.gov/PhysRefData/XrayMassCoef/ElemTab/z14.html')
    soup = BeautifulSoup(req.text, 'lxml')
    lista = soup.find_all('pre')
    print(lista)

With this code I request and already separate what I need via Beautiful Soup. The answer I hope comes in the following list:

[

____________________________________

 <b>Energy</b>       <i>μ</i>/<i>ρ</i>        <i>μ</i><sub>en</sub>/<i>ρ</i> 

 <sup> </sup>(MeV)      (cm<sup>2</sup>/g)     (cm<sup>2</sup>/g)

1.00000E-03 1.570E+03 1.567E+03

1.50000E-03 5.355E+02 5.331E+02

1.83890E-03 3.092E+02 3.070E+02

K 1.83890E-03 3.192E+03 3.059E+03

2.00000E-03 2.777E+03 2.669E+03

3.00000E-03 9.784E+02 9.516E+02

4.00000E-03 4.529E+02 4.427E+02

5.00000E-03 2.450E+02 2.400E+02

6.00000E-03 1.470E+02 1.439E+02

8.00000E-03 6.468E+01 6.313E+01

1.00000E-02 3.389E+01 3.289E+01

1.50000E-02 1.034E+01 9.794E+00

2.00000E-02 4.464E+00 4.076E+00

3.00000E-02 1.436E+00 1.164E+00

4.00000E-02 7.012E-01 4.782E-01

5.00000E-02 4.385E-01 2.430E-01

6.00000E-02 3.207E-01 1.434E-01

8.00000E-02 2.228E-01 6.896E-02

1.00000E-01 1.835E-01 4.513E-02

1.50000E-01 1.448E-01 3.086E-02

2.00000E-01 1.275E-01 2.905E-02

3.00000E-01 1.082E-01 2.932E-02

4.00000E-01 9.614E-02 2.968E-02

5.00000E-01 8.748E-02 2.971E-02

6.00000E-01 8.077E-02 2.951E-02

8.00000E-01 7.082E-02 2.875E-02

1.00000E+00 6.361E-02 2.778E-02

1.25000E+00 5.688E-02 2.652E-02

1.50000E+00 5.183E-02 2.535E-02

2.00000E+00 4.480E-02 2.345E-02

3.00000E+00 3.678E-02 2.101E-02

4.00000E+00 3.240E-02 1.963E-02

5.00000E+00 2.967E-02 1.878E-02

6.00000E+00 2.788E-02 1.827E-02

8.00000E+00 2.574E-02 1.773E-02

1.00000E+01 2.462E-02 1.753E-02

1.50000E+01 2.352E-02 1.746E-02

2.00000E+01 2.338E-02 1.757E-02

]

I need to save this data to a . txt or . csv file, preferably .txt. Any suggestions?

  • this will be a repetitive job? If not, why don’t you just copy and paste the site itself? If so, you will have to deal with the text before saving it to be in a suitable format. Take a look at the library Rows. The purpose of this is to extract any table of any format. I hope it helps.

1 answer

0

If you want to save the file exactly as it is in the list:

import io
with io.open('arquivo.txt', "w", encoding="utf-8") as file:
    file.write(str(lista))

Browser other questions tagged

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