我正在将CSV导入python,并希望创建列表,这些列表是CSV文件的当前列。这些列表将包含每一列下的所有数据。
我已经尝试过将随该问题发布的代码。
import os
import csv
import numpy as np
import pandas as pd
os.chdir('My Drive Information HERE')
os.getcwd()
infile = 'diamonds.csv'
# create new empty list
diamondList = []
with open(infile, 'rU') as csvfile:
# the csv file reader returns a list of the csv items on each line
ALReader = csv.reader(csvfile, dialect='excel', delimiter='\t')
# from each line, a list of row items, put each element in a dictionary
# with a key representing the data
for line in ALReader:
# skip lines without data
if line[0] == '':
continue
else:
try:
# create a dictionary for each player
diamonds = {}
# add each piece of data under a key representing that data
diamonds['diamond carat'] = line[0]
diamonds['diamond cut'] = line[1]
diamonds['diamond color'] = line[2]
diamonds['diamond clarity'] = line[3]
diamonds['diamond price'] = line[4]
# add this player to the list
diamondList.append(diamonds)
except IndexError:
print ('Error: ', line)
csvfile.close()
print ("Read", len(diamondList), "diamond data")
我希望每个标准(例如钻石克拉)都将在列表中包含相关数据。