我试图通过csv文件进行解析,并仅从特定列中提取数据。

例csv:

ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS |
10 | C... | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |

我试图只捕获特定的列,比如ID、Name、Zip和Phone。

我看过的代码让我相信我可以通过对应的数字调用特定的列,因此ie: Name将对应于2,并且使用行[2]遍历每一行将产生列2中的所有项。但事实并非如此。

以下是我目前所做的:

import sys, argparse, csv
from settings import *

# command arguments
parser = argparse.ArgumentParser(description='csv to postgres',\
 fromfile_prefix_chars="@" )
parser.add_argument('file', help='csv file to import', action='store')
args = parser.parse_args()
csv_file = args.file

# open csv file
with open(csv_file, 'rb') as csvfile:

    # get number of columns
    for line in csvfile.readlines():
        array = line.split(',')
        first_item = array[0]

    num_columns = len(array)
    csvfile.seek(0)

    reader = csv.reader(csvfile, delimiter=' ')
        included_cols = [1, 2, 6, 7]

    for row in reader:
            content = list(row[i] for i in included_cols)
            print content

我期望它只打印出每行我想要的特定列,但它没有,我只打印出最后一列。


import csv
from collections import defaultdict

columns = defaultdict(list) # each value in each column is appended to a list

with open('file.txt') as f:
    reader = csv.DictReader(f) # read rows into a dictionary format
    for row in reader: # read a row as {column1: value1, column2: value2,...}
        for (k,v) in row.items(): # go over each column name and value 
            columns[k].append(v) # append the value into the appropriate list
                                 # based on column name k

print(columns['name'])
print(columns['phone'])
print(columns['street'])
      

像这样的文件

name,phone,street
Bob,0893,32 Silly
James,000,400 McHilly
Smithers,4442,23 Looped St.

将输出

>>> 
['Bob', 'James', 'Smithers']
['0893', '000', '4442']
['32 Silly', '400 McHilly', '23 Looped St.']

或者如果你想对列进行数字索引:

with open('file.txt') as f:
    reader = csv.reader(f)
    next(reader)
    for row in reader:
        for (i,v) in enumerate(row):
            columns[i].append(v)
print(columns[0])

>>> 
['Bob', 'James', 'Smithers']

要更改分隔符,请将delimiter=" "添加到适当的实例化,即reader = csv。读者(f,分隔符= " ")


从这段代码中获得最后一列的唯一方法是在for循环中不包含print语句。

这很可能是你代码的结尾:

for row in reader:
    content = list(row[i] for i in included_cols)
print content

你希望它是这样的:

for row in reader:
        content = list(row[i] for i in included_cols)
        print content

现在我们已经解决了你的错误,我想借此时间向你介绍熊猫模块。

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

import pandas as pd
df = pd.read_csv(csv_file)
saved_column = df.column_name #you can also use df['column_name']

所以如果你想保存你列Names中的所有信息到一个变量中,这就是你所需要做的:

names = df.Names

这是一个很棒的模块,我建议你研究一下。如果由于某种原因,你的打印语句在for循环中,它仍然只打印出最后一列,这是不应该发生的,但如果我的假设是错误的,请告诉我。你发布的代码有很多缩进错误,所以很难知道什么应该在哪里。希望这对你有帮助!


您可以使用numpy.loadtext(文件名)。例如,如果这是你的数据库。csv:

ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS |
10 | Adam | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |
10 | Carl | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |
10 | Adolf | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |
10 | Den | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |

你需要Name列:

import numpy as np 
b=np.loadtxt(r'filepath\name.csv',dtype=str,delimiter='|',skiprows=1,usecols=(1,))

>>> b
array([' Adam ', ' Carl ', ' Adolf ', ' Den '], 
      dtype='|S7')

你可以更容易地使用genfromtext:

b = np.genfromtxt(r'filepath\name.csv', delimiter='|', names=True,dtype=None)
>>> b['Name']
array([' Adam ', ' Carl ', ' Adolf ', ' Den '], 
      dtype='|S7')

Context: For this type of work you should use the amazing python petl library. That will save you a lot of work and potential frustration from doing things 'manually' with the standard csv module. AFAIK, the only people who still use the csv module are those who have not yet discovered better tools for working with tabular data (pandas, petl, etc.), which is fine, but if you plan to work with a lot of data in your career from various strange sources, learning something like petl is one of the best investments you can make. To get started should only take 30 minutes after you've done pip install petl. The documentation is excellent.

答:假设您在csv文件中有第一个表(您也可以使用petl直接从数据库加载)。然后您只需加载它并执行以下操作。

from petl import fromcsv, look, cut, tocsv 

#Load the table
table1 = fromcsv('table1.csv')
# Alter the colums
table2 = cut(table1, 'Song_Name','Artist_ID')
#have a quick look to make sure things are ok. Prints a nicely formatted table to your console
print look(table2)
# Save to new file
tocsv(table2, 'new.csv')

对于pandas,你可以使用read_csv和usecols参数:

df = pd.read_csv(filename, usecols=['col1', 'col3', 'col7'])

例子:

import pandas as pd
import io

s = '''
total_bill,tip,sex,smoker,day,time,size
16.99,1.01,Female,No,Sun,Dinner,2
10.34,1.66,Male,No,Sun,Dinner,3
21.01,3.5,Male,No,Sun,Dinner,3
'''

df = pd.read_csv(io.StringIO(s), usecols=['total_bill', 'day', 'size'])
print(df)

   total_bill  day  size
0       16.99  Sun     2
1       10.34  Sun     3
2       21.01  Sun     3

要获取列名,最好使用readline()而不是readlines(),以避免循环&读取完整文件并将其存储在数组中。

with open(csv_file, 'rb') as csvfile:

    # get number of columns

    line = csvfile.readline()

    first_item = line.split(',')

使用熊猫:

import pandas as pd
my_csv = pd.read_csv(filename)
column = my_csv.column_name
# you can also use my_csv['column_name']

在解析时丢弃不需要的列:

my_filtered_csv = pd.read_csv(filename, usecols=['col1', 'col3', 'col7'])

附注:我只是以一种简单的方式汇总其他人说过的话。实际答案是从这里和这里取的。


由于你可以索引和子集pandas数据框架,一个非常简单的方法从csv文件提取单列到一个变量是:

myVar = pd.read_csv('YourPath', sep = ",")['ColumnName']

有几件事需要考虑:

上面的代码片段将生成一个pandas系列,而不是数据框架。 如果速度是一个问题,ayhan和usecols的建议也会更快。 在一个2122 KB大小的csv文件上使用%timeit测试这两种不同的方法,usecols方法得到22.8 ms的结果,而我建议的方法得到53 ms的结果。

别忘了进口熊猫当pd


如果需要单独处理列,我喜欢使用zip(*iterable)模式(有效地“unzip”)来解构列。举个例子:

ids, names, zips, phones = zip(*(
  (row[1], row[2], row[6], row[7])
  for row in reader
))

import pandas as pd 
csv_file = pd.read_csv("file.csv") 
column_val_list = csv_file.column_name._ndarray_values

我认为有一个更简单的方法

import pandas as pd

dataset = pd.read_csv('table1.csv')
ftCol = dataset.iloc[:, 0].values

在这里iloc[:, 0],:表示所有值,0表示列的位置。 在下面的例子中,ID将被选中

ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS |
10 | C... | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |

SAMPLE.CSV
a, 1, +
b, 2, -
c, 3, *
d, 4, /
column_names = ["Letter", "Number", "Symbol"]
df = pd.read_csv("sample.csv", names=column_names)
print(df)
OUTPUT
  Letter  Number Symbol
0      a       1      +
1      b       2      -
2      c       3      *
3      d       4      /

letters = df.Letter.to_list()
print(letters)
OUTPUT
['a', 'b', 'c', 'd']

import pandas as pd

dataset = pd.read_csv('Train.csv')
X = dataset.iloc[:, 1:-1].values
y = dataset.iloc[:, -1].values

X是一堆列,如果你想读更多的列,就用它 Y是单列,用它来读一列 [:, 1:-1]是[row_index: to_row_index, column_index: to_column_index]


import csv

with open('input.csv', encoding='utf-8-sig') as csv_file:
    # the below statement will skip the first row
    next(csv_file)
    reader= csv.DictReader(csv_file)
   
    Time_col ={'Time' : []}
    #print(Time_col)
    for record in reader :
        Time_col['Time'].append(record['Time'])
        print(Time_col)

从CSV文件读写,您可以导入CSV并使用以下代码:

with open('names.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['first_name'], row['last_name'])