我已经生成了一个csv文件,它看起来像这样:
50,57,13,10,50,48,13,10,49,55,13,10,49,54,13,10,49,52,13,10,49,52,13,10,49,50,13,10,49,49,13,10
49,49,13,10,57,13,10,57,13,10,57,13,10,56,13,10,56,13,10,55,13,10,54,13,10,54,13,10,54,13,10,54
13,10,54,13,10,54,13,10,54,13,10,53,13,10,54,13,10,54,13,10,54,13,10,54,13,10,54,13,10,53,13,10
53,13,10,52,13,10,52,13,10,52,13,10,53,13,10,53,13,10,53,13,10,52,13,10,51,13,10,52,13,10,52,13
10,52,13,10,53,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51
13,10,51,13,10,52,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,52,13,10,52,13,10,53,13,10
53,13,10,51,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13
10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51
13,10,52,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10
52,13,10,51,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,50,13,10,51,13,10,51,13
10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,52,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51
13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13,10,53,13,10,52,13,10
52,13,10,51,13,10,52,13,10,51,13,10,52,13,10,52,13,10,52,13,10,51,13,10,51,13,10,51,13,10,52,13我想重新构造它,这样它应该只有一行,根本没有列。我试过numpy.genfromtxt
new=np.genfromtxt('repaired.csv', dtype='float', delimiter=',', skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars='"', replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None)但它并没有起作用。我得到的错误是:
ValueError: Some errors were detected !
Line #1591 (got 28 columns instead of 32)
Line #1593 (got 4 columns instead of 32)发布于 2017-06-22 17:36:43
为了将csv文件转换为numpy array,我已经完成了所有这些工作
import csv
import numpy as np
data_in_csv_file = []
# reading the csv file
with open('hello2.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
data_in_csv_file.append(row)
# removing the empty row of the csv file
# and convert into a list
list_values = sum(data_in_csv_file,[])
# converting numpy array
values = np.array(list_values)
print(values)发布于 2017-06-22 17:07:30
如果你有可能使用pandas,你可以尝试这样做:
import pandas
new = pandas.read_csv('repaired.csv', sep=',', engine='python', header=None)csv中的第一行必须是最长的行之一,否则这也不会起作用。
如果您需要将数据转换为纯numpy数组,您可以将其转换为:
nm = new.as_matrix()https://stackoverflow.com/questions/44694529
复制相似问题