<html>
<head>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<title></title>
</head>
<body moz_template="id11" bgcolor="#ffffff" text="#000000">
Hola Luis. Gracias por contestarme.<br>
<br>
Verás row[2] y 5 se guardan en una lista para ir comprobando si
existe la linea. Pero no se guarda.<br>
<br>
Lo que se guarda es row tal como se lee:<br>
if s in cm:<br>
cn += 1<br>
else:<br>
cm.append(s)<br>
swriter.writerow(row)<br>
<br>
Es decir si s (row[2] y row[5]) no esta en la lista cm (donde van
estos dos registros unidos.) Entonces garda s en la lista cm para
comprobar más adelante y guarda row tal como se leyó en el archivo
de salida.<br>
<br>
Pero si ves, row no ha sufrido modificación alguna. Eso es lo que me
desconcierta. Por cierto probé swriter.writerow(row[:-1]) y no ha
funcionado.<br>
<br>
¿ que podrá ser ?<br>
<br>
Gracias.<br>
<br>
<br>
<div style="color: black;">
<div class="moz-cite-prefix">El 16/10/2012 13:22, Luis Garcia
Merino escribió:<br>
</div>
<blockquote
cite="mid:1350386554.98975.YahooMailNeo@web171203.mail.ir2.yahoo.com"
type="cite">
<div style="color:#000; background-color:#fff;
font-family:arial, helvetica, sans-serif;font-size:10pt">Buenas,<br>
<br>
supongo que es porque el archivo CSV, al final de cada fila,
tiene un salto de línea. Imagino también que row[5] es la
última columna del CSV y, por lo tanto, tiene ese salto de
línea. Podrías solucionarlo haciendo:<br>
<br>
s = row[2]+" "+row[5][:-1]<br>
<br>
en vez de <br>
<div><span><br>
</span></div>
<div>s = row[2]+" "+row[5]</div>
<div><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;">Yo uso esto para limpiar
las celdas de un CSV (y pasarlas a utf-8):</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;">def
unicode_csv_reader(unicode_csv_data, dialect=csv.excel,
**kwargs):<br>
<br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><span class="tab"> </span>csv_reader
= csv.reader(unicode_csv_data,dialect=dialect, **kwargs)<br>
<br>
for row in csv_reader:</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><span class="tab"> </span><span
class="tab"> # en el yield puedes hacer lo que quieras
con la celda... </span>por ejemplo .strip() o el lower()
que haces tu<br>
yield [unicode(cell.replace("\n",""), 'utf-8') for
cell in row]<br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div><br>
</div>
<div>def haz_lo_que_sea_con_el_csv( <span class="tab">csv_file</span>
):</div>
<div><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><span class="tab"> f =
open(</span><span class="tab"><span class="tab">csv_file</span>,
'r')<br>
</span></div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><span class="tab">
csv_reader = unicode_csv_reader( f )</span></div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
<span class="tab"></span></div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><span class="tab"> #
con esto lees una línea</span></div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><span class="tab"> row</span>
= csv_reader.next()</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;">No sé si es la mejor
manera de hacerlo, pero funciona :)<br>
<span class="tab"></span><span class="tab"></span></div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;">Un saludo.<br>
</div>
<div style="color: rgb(0, 0, 0); font-size: 13.3333px;
font-family: arial,helvetica,sans-serif; background-color:
transparent; font-style: normal;"><br>
</div>
<div style="font-family: arial, helvetica, sans-serif;
font-size: 10pt;">
<div style="font-family: times new roman, new york, times,
serif; font-size: 12pt;">
<div dir="ltr"> <font size="2" face="Arial">
<hr size="1"> <b><span style="font-weight:bold;">De:</span></b>
kausdiv <a class="moz-txt-link-rfc2396E" href="mailto:kausdiv@gmail.com"><kausdiv@gmail.com></a><br>
<b><span style="font-weight: bold;">Para:</span></b>
La lista de python en castellano
<a class="moz-txt-link-rfc2396E" href="mailto:python-es@python.org"><python-es@python.org></a> <br>
<b><span style="font-weight: bold;">Enviado:</span></b>
Martes 16 de octubre de 2012 12:56<br>
<b><span style="font-weight: bold;">Asunto:</span></b>
[Python-es] Salto de linea<br>
</font> </div>
<br>
Hola.<br>
<br>
os comento algo que me pasa casi siempre que uso ficheros.
:-(<br>
<br>
Tengo un archivo CSV donde hay muchas lineas repetidas. He
preparado el siguiente programa que busca los registros
repetidos.<br>
La idea es que se crea un nuevo archivo "bb.css" donde van
todos los registros menos los repetidos.<br>
<br>
El problema es que se agrega un salto de linea que no
debería ("\n") de forma que el resultado es algo as:<br>
---------<br>
linea ok<br>
<br>
linea ok<br>
---------<br>
Entre cada linea ok hay una linea en blanco.<br>
<br>
Este es el programa:<br>
-----------------------------------------<br>
import csv<br>
<br>
cn=0 # para contar reptediso<br>
farchi="aa.csv" # archivo con todos los registros<br>
cm=[] # Para poder conocer los repetidos.<br>
<br>
f=open("bb.csv","w") # Archivo destino con los registros
sin repetir.<br>
swriter = csv.writer(f, delimiter=',')<br>
<br>
with open(farchi, 'rb') as csvfile:<br>
spamreader = csv.reader(csvfile, delimiter=',')<br>
for row in spamreader:<br>
if len(row)>5:<br>
s = row[2]+" "+row[5]<br>
s = s.lower()<br>
if s in cm:<br>
cn += 1<br>
else:<br>
cm.append(s)<br>
swriter.writerow(row)<br>
<br>
f.close()<br>
<br>
print "Registros duplicados ",cn<br>
_______________________________________________<br>
Python-es mailing list<br>
<a moz-do-not-send="true"
ymailto="mailto:Python-es@python.org"
href="mailto:Python-es@python.org">Python-es@python.org</a><br>
<a moz-do-not-send="true"
href="http://mail.python.org/mailman/listinfo/python-es"
target="_blank">http://mail.python.org/mailman/listinfo/python-es</a><br>
FAQ: <a moz-do-not-send="true"
href="http://python-es-faq.wikidot.com/" target="_blank">http://python-es-faq.wikidot.com/</a><br>
<br>
<br>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Python-es mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Python-es@python.org">Python-es@python.org</a>
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/python-es">http://mail.python.org/mailman/listinfo/python-es</a>
FAQ: <a class="moz-txt-link-freetext" href="http://python-es-faq.wikidot.com/">http://python-es-faq.wikidot.com/</a>
</pre>
</blockquote>
<br>
</div>
<br>
<br>
</body>
</html>