【发布时间】:2021-07-13 06:31:57
【问题描述】:
我一直在使用 Pandas/Python 在 Mac 中读取管道分隔的数据文件。数据文件包含没有标题的聊天记录。我们可以提供的标头信息是ID | Name | transcripts_text
示例文本文件为:
P37001 |约翰 |感谢您伸出援手。我们的客户支持代理can&rsquo;t(不能)接受所有申请或通过我们的任何联系渠道(包括电话和消息传递)提供状态。<br />
<br />
您可以使用我们的<a href="https://www.google.com/"> <span style="color: rgb(0, 0, 255);">Payment Program</span></a> 申请贷款。要查看您的贷款状态或有其他问题,请转至the&nbsp;
<a href="https://google.com/" target="_blank">``<span style="color: rgb(0, 0, 255);">支付计划</span></a>.<br /> &nbsp;
<p>To apply for loan on your check Program loan, visit <a href="http://google.com/" ``target="_blank"><span style="color: rgb(0, 0, 255);">google.com/</span></a>.</p>
<br />
<br />
&nbsp;
P37002 |约翰 | <span style="color: rgb(0, 0, 205);"><strong>XX FORMAL MALE XX</strong></span><br />
为了收集您的个人信息,我们将在接下来的几分钟内提供一份安全表格。当您看到消息框变为安全表单时,您可以安全输入
&quot;Submit&quot;。选择“提交”后,消息框将返回,您可以在我们的对话中输入非个人性质的其他详细信息。<br />
<br />
<span style="color: rgb(0, 0, 205);"><strong>XX FORMAL FEMALE XX</strong></span>``<br />
为了收集您的个人信息,我们将在接下来的几分钟内提供一个安全的网络表单。当您看到消息框更改为安全网络表单时,您可以安全地输入您的个人数据并选择&quot;Submit&quot;。选择&quot;Submit&quot; 后,消息框将返回,您可以在我们的对话中输入其他非个人性质的详细信息。
<br />
<br />
<br />
代码:
import pandas as pd
quick_reply=pd.read_csv('final.txt', names=['ID','Name','transcripts_text'], sep='|')
输出:
ID Name transcripts_text
P37001 John Thanks for reaching out. Out..
<br /> NaN NaN
预期输出:
ID Name transcripts_text
P37001 John Thanks for reaching out. Our customer support agents can't accept all applications or provide status through any of our contact channels, including phone and messaging.You can use our ..
Removing all the html tags. And the data is captured properly in pandas data frame considering P37001 data as single row and P37002 as second row
【问题讨论】:
-
您的文件中是否有不带引号的换行符?
-
在新 id 的开始之前有这个字符
&nbsp;,有时它在那里,有时它不在那里。所以没有明确的分隔符来识别新行(我的意思是新的ID) -
请尝试从您的问题中消除混乱。有关How to ask a good question? 的更多信息,请阅读本文!
-
编辑样本数据
-
我不是在谈论转义的 HTML 实体;您的数据中似乎有换行符。但是,这就是分隔不同记录的方式。
标签: python python-3.x pandas csv