gpt4 book ai didi

python - 有什么方法可以将 txt 文件列表(带有分割线)合并到 pandas 数据框中吗?

转载 作者:行者123 更新时间:2023-12-01 08:18:34 31 4
gpt4 key购买 nike

我有一堆假新闻文本的 txt 文件,我想将它们放入 pandas 数据框中。但是,在单个文本文件中,第一行和第三行是分开的,这对于所有 csv 文件都是相同的。但是,我希望它们位于 pandas 数据框中以进行简单的数据操作。我尝试对我的数据执行以下操作:

cols, vals = zip(*[line.split('\n') for line in StringIO("biz01.fake.txt").read().split('\n\n')])
s = pd.Series(vals, cols)
s.index = [s.groupby(level=0).cumcount(), s.index]
s.unstack()

但是我遇到了一个错误,我不明白为什么。

ValueError: not enough values to unpack (expected 2, got 1)

谁能帮我实现这个目标吗?有什么解决方案可以完成此任务吗?

这是我的原始数据:

input txt files

这是 txt 文件列表(在我的本地驱动器上)的样子: list of txt files

所需输出示例

1   first_row     second_row
2 headers_1 some text
3 headers_2 some texts

我将一些示例文本文件放在 flies 上。谁能帮助我如何完成这件事?有什么想法吗?

新更新:

当我尝试以下解决方案时,我得到了这种类型的输出:

new output

在我的输入txt文件中,第二行是一个非常长的文本,我只想要具有默认列名称的两列数据框。还有更好的主意吗?

最佳答案

您可以使用 pandas 读取文件,然后删除空行。

例如,这是一个文本文件“news.txt”

Alex Jones Vindicated in "Pizzagate" Controversy

"Alex Jones, purveyor of the independent investigative news website Infowars and host of The Alex Jones Show, has been vindicated in his claims regarding the so-called "Pizzagate" controversy. Jones and others uncovered evidence last year that top Democratic Party officials were involved in a bizarre, satanic child sex cult and pornography ring using the Washington D.C. pizza parlor Comet Ping Pong Pizza as a front. The allegations rocked the Democratic Party and may have caused serious damage to the Hillary Clinton presidential campaign. Top U.S. federal investigators have now confirmed that they have verified many of these claims after executing raids on the offices of several of the key players. Charges are expected to be filed in the coming days. The news comes as a welcome vindication for Jones, who has been accused by many mainstream media outlets of being a conspiracy theorist and of publishing "fake news". Mr. Jones has often drawn controversy, and was scapegoated in media reports as an example of how inaccurate and misleading news proliferated on social media websites like Facebook, Youtube and Twitter during the 2016 election. Jones has also exposed inconsistencies in the official government accounts of the 9/11 terrorist attacks and the Sandy Hook school shooting in Newtown Connecticut."

然后我使用代码

df = pd.read_csv('news.txt', header = None, sep = '\n', skip_blank_lines = True)
#df.dropna(axis = 0, how = 'all', inplace = True)
df = df.T
df.columns = ['Header', 'Rows']

输出

                                              Header    Rows
0 Alex Jones Vindicated in "Pizzagate" Controversy Alex Jones, purveyor of the independent invest...

关于python - 有什么方法可以将 txt 文件列表(带有分割线)合并到 pandas 数据框中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54835602/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com