Python, CSV, skipping lines based on content -


I am handling CSV files with buffer rows before the header, the number of rows changes and some rows are stringed Nothing is nothing but the one thing that is consistent is that one or more cells in these buffer rows have zero values, so I am trying to release any line from a null cell.

I've got this scripting outputting a blank file on the following script. I am feeling that I am not getting 'and' but I am feeling that if I put it in a loop, then I will prepare a file for each line ...

 Outf ("out_" + fileName, 'wb') in the form of inf (filename, 'rb') as outf: csvreader = csv.DictReader (inf) if 'csvreader.fieldnames' In: next (csvreader) else: fieldnames = [name 'url_source', 'downloaded_at'] + csvreader.fieldnames # csvwriter = csv.DictWriter (outf, field name) for csvwriter.writeheader () Add column name to node, in enumerate Line (csv Reader, 1): csvwriter .writerow (dict (line, url_source = csvUrl, downloaded_at = today))  

Your code a object; Either it will read and leave a line, then back, or will read this whole file and copy it to a new CSV. Both of them will never do

If you can not rely on the first row of the header, then do not rely on DictReader () field names to auto-load. Find the headers manually, then go to DictReader () constructor on them.

Open as a regular csv.reader () , first , find the first line to find that real line, then type the file to Reload as csv.DictReader () as a field name clearly as: Asf opened (file name, 'rb') with < / P>

  open ("out _" + filename, 'wb') outf: reader = csv.reader (inf) # Line for the line in the reader as the header: if not in the '' line Is: field name = row Breaking Second Second: # Ops, * Only * Rows with empty values ​​value value ('unable to set header row)' # Rewind, switch to dict reader, headline inf.seek for line in previous read reader (0) reader = csv.DictReader (inf, fieldnames): If all the rows with row.keys () == row.values ​​() break # extra two columns author = csv.DictWriter (outf, [ 'Url_source', 'downloaded' '+ name of field)  
author Author (Author) (Author (Author (Author (R, URL_Source = CSU URL, Download_T = = Today) for R in Reader)

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

c# - Create dynamic LINQ query expression at runtime which translates into a parameterized SQL query for nullable types -

cmake - cpack generates RPMs with %files entries that conflict with the RPM spec. How to fix? -