2017-06-22 27 views
0

的:編輯我已經生成一個CSV文件,它看起來像這樣一個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

好像你的一些行可能是幾列簡短的 - 你可以導航到文件中指定的行,看看他們是否格式正確? –

+0

你想作爲'list'嗎? –

+0

錯誤信息不能更清晰...確保你所有的行有相同的長度, –

回答

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) 
1

如果你有可能使用​​你可以試試這個:

import pandas 

new = pandas.read_csv('repaired.csv', sep=',', engine='python', header=None) 

在CSV必須之中,雖然最長行的第一行,否則將不工作。

,如果你需要你的數據作爲純numpy數組,你可以把它轉換:

nm = new.as_matrix()