Форум сайта python.su
Доброй ночи!
Надо отпарсить список и убрать все пустые элементы и элементы содержащие пробелы.
Вот чего написал, почему не работает?
#!/usr/bin/python
# -*- coding:utf-8 -*-
import csv
import sys
f = open('/opt/otput.ini','r')
try:
reader = csv.reader(f)
writer = csv.writer(w)
mass = []
for row in reader:
mass = mass + row[0].split('\t')
n,m = (0,1)
while m < len(mass):
if mass[n:m]=='':
mass.remove('')
n+=1
m+=1
print mass
finally:
f.close()
['[[router Router1]]', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'model=7200', '', '', '', '', '', '', 'image=http://www.altova.com', '', '', '', '', '', '', 'slot0=C7200-IO-2FE', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'f0/0=Router2 f0/0', '', '', '', '', '', '', '', 'f0/1=Router3 f0/1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'slot1=PA-FE-TX', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '[[router Router2]]', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'model=3660', '', '', '', '', '', '', 'image=http://www.altova.com', '', '', '', '', '', 'f0/0=Router1 f0/0', '', '', '', '', '', 'f0/1=Router3 f0/0', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '[[router Router3]]', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'model=7200', '', '', '', '', '', '', 'image=http://www.altova.com', '', '', '', '', '', '', 'slot0=C7200-IO-2FE', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'f0/0=Router2 f0/1', '', '', '', '', '', '', '', 'f0/1=Router1 f0/1']
Офлайн
Ой, не погуглил сразу ;-)
Вот решение: mass = filter(lambda i: i != '', mass)
Metheo
Доброй ночи!
Надо отпарсить список и убрать все пустые элементы и элементы содержащие пробелы.
Вот чего написал, почему не работает?Выводит мне список:#!/usr/bin/python
# -*- coding:utf-8 -*-
import csv
import sys
f = open('/opt/otput.ini','r')
try:
reader = csv.reader(f)
writer = csv.writer(w)
mass = []
for row in reader:
mass = mass + row[0].split('\t')
n,m = (0,1)
while m < len(mass):
if mass[n:m]=='':
mass.remove('')
n+=1
m+=1
print mass
finally:
f.close()['[[router Router1]]', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'model=7200', '', '', '', '', '', '', 'image=http://www.altova.com', '', '', '', '', '', '', 'slot0=C7200-IO-2FE', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'f0/0=Router2 f0/0', '', '', '', '', '', '', '', 'f0/1=Router3 f0/1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'slot1=PA-FE-TX', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '[[router Router2]]', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'model=3660', '', '', '', '', '', '', 'image=http://www.altova.com', '', '', '', '', '', 'f0/0=Router1 f0/0', '', '', '', '', '', 'f0/1=Router3 f0/0', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '[[router Router3]]', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'model=7200', '', '', '', '', '', '', 'image=http://www.altova.com', '', '', '', '', '', '', 'slot0=C7200-IO-2FE', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'f0/0=Router2 f0/1', '', '', '', '', '', '', '', 'f0/1=Router1 f0/1']
Офлайн
> Вот решение: mass = filter(lambda i: i != '', mass)
filter(str.strip, lines)
..bw
Офлайн