pandas 读为“定界".文件 [英] Pandas read "delimited" file

查看:77
本文介绍了 pandas 读为“定界".文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆