从保存的csv文件读取值时出现问题。这是我拥有的csv文件的一部分:
000000216739.jpg, 224, [ 0. 0. 0. 0. 36. 44. 4. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 14.
8. 0. 0. 0. 0. 0. 0. 0. 0. 0. 7. 0. 3. 0. 0. 0. 0. 0.
0. 0. 0. 0. 3. 1. 2. 0. 0. 0. 0. 0. 1. 0. 0. 1. 2. 0.
3. 0. 0. 0. 0. 0.],
[ 0. 0. 0. 0. 35. 33. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 36. ...]
(根据我拥有的csv文件格式化)
问题是,我真的不确定如何分别读取每个逗号分隔的值。当我:
with open(CSVFilepath) as f:
reader = csv.reader(f,delimiter=',')
for row in reader:
print(row)
print(row[0])
print(row[1])
print(row[2])
它返回:
['000000216739.jpg', '224', '[ 0. 0. 0. 0. 36. 44. 4. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 14.']
000000216739.jpg
224
[ 0. 0. 0. 0. 36. 44. 4. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 14. ]
值224
实际上是图像000000216739.jpg
中[]秒(行)的数量。
我要读取的是一个2d numpy形状的数组(224,60),其中60固定用于所有图像。
所以我想读的是: 例如图像123.jpg(所有形状均为一形(224 x 60):
[[ 0. 0. 0. 0. 36. 44. 4. 0. 0. 0. 0. 0. 0. 0. 0. 0. 9. 14.
8. 0. 0. 0. 0. 0. 0. 0. 0. 0. 7. 0. 3. 0. 0. 0. 0. 0.
0. 0. 0. 0. 3. 1. 2. 0. 0. 0. 0. 0. 1. 0. 0. 1. 2. 0.
3. 0. 0. 0. 0. 0.],
... (more np arrays)...
[ 6. 0. 0. 35. 64. 0. 0. 0. 0. 0. 0. 0. 20. 11. 27. 23. 5. 0.
0. 0. 0. 0. 0. 0. 5. 0. 10. 1. 0. 0. 0. 0. 0. 0. 0. 0.
6. 2. 3. 0. 0. 0. 0. 0. 0. 0. 0. 0. 2. 2. 1. 0. 0. 0.
0. 0. 0. 0. 0. 0.]]
请问我该怎么办..?另外,该文件非常大,我需要一种有效读取文件的方法...非常感谢您的帮助!
答案 0 :(得分:0)
我认为您的数据是这样的;文件名,行,[imrow1],[imrow2],[imrow3],...,[imrow_nrows],然后执行以下操作从数据中提取图像。让我们知道您的数据格式是否不同。
with open(CSVFilepath) as f:
reader = csv.reader(f,delimiter=',')
for row in reader:
fname=row[0]
nrows=int(row[1])
imdata=np.array(row[2:]) #Get from the 2 element to the end. Could also do row[:-nrows]
#might need to make each element an int or float.
###reshape if that's what you need and do something with the image
答案 1 :(得分:0)
您的文件不是正确的csv文件,因此您不应像csv文件那样阅读它。
csv文件中的换行符代表一个新行,但是很明显,在文件中它们并不意味着-您想读取#include <iostream>
using namespace std;
template<typename T>
class Stack{
private:
class Node{
friend Stack;
public:
void operator++(){
this->next = this->next->next; //point to next Node
}
bool operator!=(const Node& rhs){
return !(*this == rhs);
}
T operator*(){
return this->next->elem; //return current Node elem
}
bool operator==(const Node& rhs){
return this->next == rhs.next;
}
private:
T elem;
Node* next;
};
Node* first;
int _size;
public:
Stack():_size(0){
first = nullptr;
}
void push(T item){
Node* n = new Node;
n->elem = item;
n->next = first;
first = n;
_size++;
}
T pop(){
T item = first->elem;
Node* old_first = first;
first = first->next;
delete old_first;
_size--;
return item;
}
int size(){
return _size;
}
bool empty(){
return _size == 0;
}
Node begin(){
Node n;
n.next = first;
return n;
}
Node end(){
Node m;
m.next = nullptr;
return m;
}
~Stack(){
Node* ele_to_delete;
while(first != nullptr){
ele_to_delete = first;
first = first->next;
delete ele_to_delete;
}
}
Stack(const Stack&) = delete;
Stack& operator=(const Stack&) = delete;
};
int main(){
Stack<int> ls;
ls.push(1);
ls.push(2);
ls.push(3);
for(auto s: ls){
cout << s << "|";
}
return 0;
}
和[
中的数字,但它们没有定界正确地
剖析此文件的一种方法是
]
我广泛使用了列表理解功能,所以这不会达到30行。尝试运行此。