简体   繁体   中英

Extract data from txt website with pandas

How could I extract the data in this link and transform it into a pandas DataFrame?

https://ticdata.treasury.gov/Publish/slt1d.txt

I've tried to use

import pandas as pd
df = pd.read_table('https://ticdata.treasury.gov/Publish/slt1d.txt', skiprows=7, sep="\s*")

But this piece of code is throwing me

ParserError: Expected 33 fields in line 18, saw 39. Error could possibly be due to quotes being ignored when a multi-char delimiter is used.

If I only use

df = pd.read_table('https://ticdata.treasury.gov/Publish/slt1d.txt')

Then it brings me the wrong data.

TABLE 1D: U.S. Long-Term Securities Held by Foreign Residents in November 2020
0   ...
1   Total...
2   Long...
3   Secur...
4   ...
5   -----...
6   Europe
7   Albania ...
8   Armenia ...
9   Austria 2...
10  Azerbaijan 1...
11  Belarus ...
12  Belgium 92...
13  Bulgaria ...
14  Croatia ...
15  Cyprus ...
16  Czech Republic 2...
17  Denmark 18...
18  Estonia ...
19  Finland 5...
20  France 42...

The read_table function is required two most like txt or csv file. You have put the address form in txt file and not using delimiter feature.That is the reason rise some error in program

Sample Code

data=pd.read_table("filename. csv or txt",delimiter=',')
data.head()

It is helpful to read the data from table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM