连续两次打印DictReader列表会产生不同的结果

时间:2020-03-14 18:49:51

标签: python csv

我正在使用import { ActionsObservable } from 'redux-observable'; import { TestScheduler } from 'rxjs/testing'; // your epics to be tested import epics from './stock'; // actions that you epic will emit import { getAll, getAllStart, getAllSuccess, } from '../actions/stock'; // mock the state param (if your epic use it) import { initialState } from '../reducers/stock'; const state = { value: { stock: initialState, }, }; // mock your dependencies param const dependencies = { api: {}, stock: { getAll() { // in my real code, this is a promise, here is just a normal function, perfect to be use with the of operator return []; }, }, Error: class Error {}, }; describe('stock epic', () => { it('should return all products', (done) => { // this is just copy-paste const testScheduler = new TestScheduler((actual, expected) => { expect(actual).toStrictEqual(expected); }); testScheduler.run(({ cold, hot, expectObservable, expectSubscriptions, flush }) => { expectObservable( epics.getAll$( ActionsObservable.of(getAll()), // transform action to action$ state, // pass mocked state dependencies // pass mocked dependencies ) ).toBe( // my epic has a delay(1000) call 'a 999ms (b|)', { a: getAllStart(), b: getAllSuccess() } ); done(); }); }); }); 模块来使用csv读取csv文件。我是Python的新手,以下行为使我感到困惑。

编辑:之后请参阅原始问题。

csv.DictReader

csv = csv.DictReader(csvFile) print(list(csv)) # prints what I would expect, a sequence of OrderedDict's print(list(csv)) # prints an empty list... 是否以某种方式突变了list

原始问题:

csv

def removeFooColumn(csv): for row in csv: del csv['Foo'] csv = csv.DictReader(csvFile) print(list(csv)) # prints what I would expect, a sequence of OrderedDict's removeFooColum(csv) print(list(csv)) # prints an empty list... 函数中的序列发生什么了?

1 个答案:

答案 0 :(得分:1)

csv.DictReader generator 迭代器,只能使用一次。解决方法:

def removeFooColumn(csv):
  for row in csv:
    del row['Foo']

csv = list(csv.DictReader(csvFile))
print(csv) # prints what I would expect, a sequence of OrderedDict's
removeFooColumn(csv)
print(csv) # prints an empty list...