import pandas as pd
import numpy as np
import os
os.listdir()
['.ipynb_checkpoints', 'Untitled.ipynb', '文本数据.txt']
wenben = pd.read_table("./文本数据.txt",sep="@",engine="python",encoding="utf8")
wenben
需求:我们可以看到,该数据读取出来的时候身边都出现了"|“符号,不过read_table函数读取文本的时候只能使用单间隔符,所以我们需要考虑将数据中的多间隔符”|@|“转换为单间隔符”@"
with open ("./文本数据.txt","r",encoding="utf8") as fpr:
content = fpr.read()
content = content.replace("|@|","@")
fpr.close()
with open ("./文本数据1.txt","w",encoding="utf8") as fpw:
fpw.write(content)
fpw.close()
# 查看当前路径下的文件
os.listdir()
['.ipynb_checkpoints', '文本数据.txt', '文本数据1.txt', '长间隔符文本的转换与读取.ipynb']
# 再一次读取文件
wenben1 = pd.read_table("./文本数据1.txt",sep="@",engine="python",encoding="utf8")
wenben1
可以看到再次读取的数据就很干净整洁了