简体   繁体   English

使用 pandas 从 txt 网站中提取数据

[英]Extract data from txt website with pandas

How could I extract the data in this link and transform it into a pandas DataFrame?如何提取此链接中的数据并将其转换为 pandas DataFrame?

https://ticdata.treasury.gov/Publish/slt1d.txt 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. read_table function 需要两个最像 txt 或 csv 文件。 You have put the address form in txt file and not using delimiter feature.That is the reason rise some error in program您已将地址形式放在 txt 文件中,并且没有使用分隔符功能。这就是程序中出现一些错误的原因

Sample Code示例代码

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

It is helpful to read the data from table从表中读取数据很有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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