如何读取csv文件并提取特定的coulmn?

2024-09-30 04:37:31 发布

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

这是我的csv文件:

CommitId                                RefactoringType      RefactoringDetail
d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   "Pull Up Attribute  protected steps : int from class blokusgame.mi.android.hazi.blokus.GameLogic.PlayerAlgorithm to class blokusgame.mi.android.hazi.blokus.GameLogic.Player"
d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   "Pull Up Attribute  protected steps : int from class blokusgame.mi.android.hazi.blokus.GameLogic.PlayerAlgorithm to class blokusgame.mi.android.hazi.blokus.GameLogic.Player"
d38f7b334856ed4007fb3ec0f8a5f7499ee2f2b8    Pull Up Attribute   "Pull Up Attribute  protected steps : int from class blokusgame.mi.android.hazi.blokus.GameLogic.PlayerAlgorithm to class blokusgame.mi.android.hazi.blokus.GameLogic.Pla

我需要提取这个:

RefactoringDetail
"Pull Up Attribute  protected steps : int from class blokusgame.mi.android.hazi.blokus.GameLogic.PlayerAlgorithm to class blokusgame.mi.android.hazi.blokus.GameLogic.Player"
"Pull Up Attribute  protected steps : int from class blokusgame.mi.android.hazi.blokus.GameLogic.PlayerAlgorithm to class blokusgame.mi.android.hazi.blokus.GameLogic.Player"
"Pull Up Attribute  protected steps : int from class blokusgame.mi.android.hazi.blokus.GameLogic.PlayerAlgorithm to class blokusgame.mi.android.hazi.blokus.GameLogic.Player"

我试过这个代码:

import pandas as pd
df = pd.read_csv('result_refactorings.csv', sep='delimiter', header=None)
df.iloc[:,-1]

它返回所有的数据

请帮忙!你知道吗


Tags: fromattributestepspullclassandroidintmi
2条回答

Pandas对于处理csv文件非常有用,您只需要以下代码就可以读取csv并将整个列保存到一个变量中:

import pandas as pd
df = pd.read_csv('test.csv', sep=';')
refactoring_details = df['RefactoringDetail']
print(refactoring_details)

编辑:提供的文件中的分隔符是;,而不是默认的,。你知道吗

如果您只想使用内置的csv模块:

import csv
import re
third_column = []
with open("result_refactorings.csv") as csvfile:
    fixed_spaces = [re.sub(" {2,}","\t",x) for x in csvfile]
    reader = csv.DictReader(fixed_spaces, delimiter="\t")
    for row in reader:
        print(row["RefactoringDetail"])
        third_column.append(row["RefactoringDetail"])

这段代码打印出第三列,并将第三列中的每一项添加到一个列表third_column。。根据你想做的,去掉一个或另一个。你知道吗

编辑:仔细观察,你的csv输入似乎是用不均匀的空格分隔的。。实际上不是标签,这就是它看起来的样子。。添加了一个小正则表达式,用一个实际的制表符替换2个或更多的并发空格。。因为在当前状态下,它不是有效的csv。你知道吗

相关问题 更多 >

    热门问题