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

时间:2019-05-29 06:52:27

标签: python csv dataframe

这是我的csv文件:

open()

我需要提取它:

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"

它返回所有数据

请帮忙!

2 个答案:

答案 0 :(得分:1)

如果您只想使用内置的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输入是用不均匀的空格分隔的..而不是实际的制表符,这是它的样子。选项卡。。由于处于当前状态,它不是有效的csv。

答案 1 :(得分:0)

Pandas在处理csv文件方面非常出色,下面的代码将是读取csv并将整列保存到变量中所需的全部内容:

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

编辑:提供的文件中的分隔符为;,而不是默认的,