I am trying to create a header row in a result file which is the first file header row and the second file header row. For example: Legacy file title line: (column A, column B ...) Map the file title row: (column C, column D ...) and my result file should contain: (column A, column B , Column C, column D) became separated with the text of each column only. I'm having trouble in the proper way to include two as you can see below "+" is not valid. The header row will always be the first row of the file. Any suggestions would be greatly appreciated. Import CSV with Open ('legacyFile.csv', 'r') as In_leg, 'Mapping File Open as in_map as 'CSV', 'R' ('results.csv', 'Wb') as out_race: c1 = csv.DictReader (in_leg, delimiter = ',', quoting = csv.QUOTE_NONE, Field Name = []) c2 = csv.DictReader (in_map, delimiter = ',', quoting = csv .QUOTE_NONE, fieldNames = []) #set header and output file header list 1 = list (C1) header list 2 = List (C2) C 1. FieldNames = (Header List 1 [0]) C 2. Enter Header Row for FieldName = (Header List 2 [0]) #fieldnames = c1.fieldnames + c2.fieldnames # - & gt; ; These print c 1.file names can not contain c2.fieldnames c3 = csv.DictWriter (out_res, fieldnames = fieldnames) c3.writeheader () in_leg.close () in_map.close () out_res.close () .field names , you see that they are formatted and typed: So we can add these two by creating a new dictionary object, in which there is no key for To accomplish this. Then when you actually write a dictionary, make sure to use the line: We need to draw a list of the names of the dictionary objectives. Let me know that this works. c2
(pdb) c1.fieldnames {none: ['field1', ' Field2 ', ...,' fieldN ']} (PDB) type (c 1. fieldNames) & lt; Type 'dict' & gt;
any
and the value of the first set of fieldnames < / Code> + second set we can use this line:
fieldnames = {none: c1.fieldnames [none] + c2.fieldnames [none]}
c3 = csv.DictWriter (out_res, fieldnames = field name [none]) < / Code instead>
No comments:
Post a Comment