如何从python的txt文件中读取特定列?

2024-09-27 21:31:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个.txt数据集:

user_000044 2009-04-24  13:47:07    Spandau Ballet  Through The Barricades 

我要读最后两个哥伦布,斯潘道芭蕾舞团的独特和通过路障的独特。我该怎么做?你知道吗

需要创建两个数组,artists =[]tracks = [],在其中我将数据放入循环中,但我无法定义行中的文本部分。你知道吗

有人能帮我吗?你知道吗


Tags: the数据文本txt定义数组tracksthrough
3条回答

使用python且不使用第三方软件包的选项:

data = open('dataset.txt', 'r').readlines()

artists = []
tracks = []

for line in data:
    artist, track = line.split(' '*2)[-2::]
    artists.append(artist.strip())
    tracks.append(track.strip())

print artists
print tracks

输出:

['Spandau Ballet']
['Through The Barricades']

[-2::]获取每行的最后2列,如果需要,调整以获取其他列。你知道吗

最好使用pandas-模块将.txt的内容加载到一个DataFrame中,然后从那里开始。如果你不熟悉它…一个 DataFrame与使用Python可以获得的Excelsheet非常接近。pandas将为您处理读取行的操作,这样您就不必编写自己的循环。你知道吗

假设您的文本文件是四列的,以制表符分隔,则如下所示:

# IPython for demo:
import pandas as pd

df = pd.read_csv('ballet.txt', sep='\t', header=None, names=['artists', 'tracks'], usecols=[2, 3])
# usecols here limits the Dataframe to only consist the 3rd and 4th column of your .txt

您的数据帧可能看起来像:

df
# Out: 
          artists                  tracks
0  Spandau Ballet  Through The Barricades
1   Berlin Ballet               Swan Lake

按列名访问单列:

df.artists  # or by their index e.g. df.iloc[:, 0]
# Out: 
0    Spandau Ballet
1     Berlin Ballet
Name: 2, dtype: object

你现在仍然可以把数据放到数组中,但是如果你知道其他的选择,我想不出你真正想这样做的原因。你知道吗

如果文件中的列由表格分隔,则可以使用np.loadtxt(NumPy函数)执行以下操作

artists, tracks = np.loadtxt("myfile.txt", delimiter = "\t", dtype = str, usecols = [ 3, 4 ], unpack = True)

这将输出一个NumPy数组。或者,您可以将这些数组转换为常规的Python字符串列表

artists = [ str(s) for s in artists ]
tracks = [ str(s) for s in tracks ]

相关问题 更多 >

    热门问题