我正在尝试使用以下代码从目录中读取几个.csv
文件,然后将输出结果的每一行存储为矩阵的一行:
import os, fnmatch
import csv
listOfFiles = os.listdir('C:/Users/m/Desktop/csv_files')
pattern = "*.csv"
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
with open(entry, newline='') as csvfile:
spamreader = csv.reader(csvfile)
for line in spamreader:
print(line)
以上代码的输出如下:
['DATE', 'OPEN', 'HIGH', 'LOW', 'CLOSE', 'PRICE', 'YCLOSE', 'VOL', 'TICKS']
['13950309', '1000000.00', '1000000', '1000000', '1000000.00', '1000000.00', '1000000', '2100000', '74']
['13950326', '1050000.00', '1050010', '1050000', '1050001.00', '1050000.00', '1000000', '1648', '5']
['13950329', '1030200.00', '1060000', '1030200', '1044474.00', '1042265.00', '1050001', '28469', '108']
但是我喜欢从数据中删除引号,并具有以下行:
[13971116, 1020002.00, 1020002, 1020000, 1020001.00, 1020000.00, 1020002, 107, 4]
并将它们存储为矩阵的行。我该怎么办?(我有numpy
库用于矩阵工作)。
答案 0 :(得分:1)
您可以尝试以下方法吗?
import os, fnmatch
import csv
listOfFiles = os.listdir('C:/Users/m/Desktop/csv_files')
pattern = "*.csv"
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
with open(entry, newline='') as csvfile:
spamreader = csv.reader(csvfile)
for line in spamreader:
try:
print(list(map(float, line)))
except:
print(line)
答案 1 :(得分:0)
在您的代码中,line
已经是一个字符串列表-没有引号。要获得数字行的矩阵,请输入:
with open(entry, newline='') as csvfile:
spamreader = csv.reader(csvfile)
data_matrix = list([ float(n) for n in row ] for row in spamreader)
您已经完成。
答案 2 :(得分:0)
您的值不包含引号。 这里的引号只是字符串分隔符,指示值是字符串。
如果您需要数字,则需要将值转换为所需的类型,例如,如果所有值都是整数,则可以使用int(value)
您的代码将是:
import os, fnmatch
import csv
listOfFiles = os.listdir('C:/Users/m/Desktop/csv_files')
pattern = "*.csv"
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
with open(entry, newline='') as csvfile:
spamreader = csv.reader(csvfile)
for line in spamreader:
print(line)
# create a new list casting all line values to integers
line_ints = [int(val) for val in line]
print(line_ints)