def deal_txt(path): """ 读取path的txt文件,处理后返回DataFrame :param path: :return: """ with open(path,'rb') as f: result=chardet.detect(f.read())#自动检测文本编码格式 try: df = pd.read_csv(path, sep='|', header=None, encoding=result['encoding'] )怎么将上述代码加入这段代码中
时间: 2024-02-22 16:54:40 浏览: 275
dbf.rar_arcgis_arcgis python_site:www.pudn.com
您可以将上述代码放在deal_txt函数的开始部分,代码如下:
```python
import chardet
import pandas as pd
def deal_txt(path):
with open(path,'rb') as f:
result=chardet.detect(f.read())
try:
df = pd.read_csv(path, sep='|', header=None, encoding=result['encoding'])
# 在这里进行DataFrame处理
return df
except Exception as e:
print(e)
```
这样,您就可以在读取txt文件后,直接对DataFrame进行处理了。
阅读全文