我已经生成了一个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)
答案 0 :(得分:1)
如果您有可能使用pandas
,可以试试这个:
import pandas
new = pandas.read_csv('repaired.csv', sep=',', engine='python', header=None)
你的csv中的第一行必须是最长的行,否则这也行不通。
如果您需要将数据作为纯numpy
数组,则可以将其转换为:
nm = new.as_matrix()
答案 1 :(得分:0)
我已经完成了将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)