pandas 读为“定界".文件 [英] Pandas read "delimited" file
问题描述
我有这样的.txt文件,第一列表示索引,其后是一对()"中的三列,分别代表x,y和z坐标.
Hi, I have such .txt file, with the first column represent index, which is followed by three columns inside a pair of "()" representing x, y and z coordinates.
我想将此文件的前四列加载到pandas Dataframe.但是,我发现很难,因为分隔符首先是",然后是(",并且在括号内是,".
I want to load the first four columns of this file to pandas Dataframe. However, I found it's pretty hard as the delimiter is firstly " " and then "(" and inside the parenthesis there is ",".
有人可以给我一些如何处理这种情况的提示吗?
Could someone give me some hint on how to deal with such situation?
谢谢! 肖恩
推荐答案
您可以将regex pattern
用作CSV分隔符.
You can use regex pattern
as seperator of CSV.
http://pandas.pydata.org/pandas -docs/stable/generated/pandas.read_csv.html
像这样
import pandas as pd
df = pd.read_csv('Initial_Coordinate.txt', sep=r'[()]', header=None)
print(df)
但是,与其创建复杂的定界符,不如将其修复为简单的定界符,然后使用pandas进行读取.
However, rather than creating complex delimiters, it is better to fix it as a simple delimiter and then read it with pandas.
thx
这篇关于 pandas 读为“定界".文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!