Fixed inconsistent CSV writing function
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				ydeng/csvbyname/pipeline/head This commit looks good
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	ydeng/csvbyname/pipeline/head This commit looks good
				
			This commit is contained in:
		
							
								
								
									
										1
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
								
							@@ -13,6 +13,7 @@
 | 
			
		||||
                "${workspaceFolder}/tests/resources",
 | 
			
		||||
                "${workspaceFolder}/output.csv",
 | 
			
		||||
                "-r",
 | 
			
		||||
                "-n",
 | 
			
		||||
                "-p",
 | 
			
		||||
                "group_num:group(\\d)-\\w-\\d+\\.txt",
 | 
			
		||||
                "group(\\d)-(?P<sect>\\w)-(?P<patid>\\d+)\\.txt",
 | 
			
		||||
 
 | 
			
		||||
@@ -63,6 +63,7 @@ def main():
 | 
			
		||||
        "Alternatively, use named REGEX groups.",
 | 
			
		||||
        nargs="+",
 | 
			
		||||
        type=str,
 | 
			
		||||
        required=True
 | 
			
		||||
    )
 | 
			
		||||
    argparser.add_argument(
 | 
			
		||||
        "-n",
 | 
			
		||||
 
 | 
			
		||||
@@ -72,20 +72,24 @@ def collect_files(
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def write_collected_to_csv(
 | 
			
		||||
    output_path: str, collected: dict[str, dict[str, str]], property_keys: Iterable[str]
 | 
			
		||||
    output_path: str,
 | 
			
		||||
    collected: dict[str, dict[str, str]],
 | 
			
		||||
    property_keys: Iterable[str],
 | 
			
		||||
    output_basename: bool,
 | 
			
		||||
):
 | 
			
		||||
    with open(output_path, "w") as output_fd:
 | 
			
		||||
        s_property_keys = sorted(property_keys)
 | 
			
		||||
        header = ["path", *s_property_keys]
 | 
			
		||||
        header = ["path"]
 | 
			
		||||
        if output_basename:
 | 
			
		||||
            header.append("basename")
 | 
			
		||||
        header.extend(s_property_keys)
 | 
			
		||||
        writer = csv.writer(output_fd)
 | 
			
		||||
        writer.writerow(header)
 | 
			
		||||
        for full_path, properties in collected.items():
 | 
			
		||||
            writer.writerow(
 | 
			
		||||
                [
 | 
			
		||||
                    full_path,
 | 
			
		||||
                    *(
 | 
			
		||||
                        properties[k] if k in properties else "N/A"
 | 
			
		||||
                        for k in s_property_keys
 | 
			
		||||
                    ),
 | 
			
		||||
                ]
 | 
			
		||||
            row = [full_path]
 | 
			
		||||
            if output_basename:
 | 
			
		||||
                row.append(os.path.basename(full_path))
 | 
			
		||||
            row.extend(
 | 
			
		||||
                (properties[k] if k in properties else "N/A" for k in s_property_keys)
 | 
			
		||||
            )
 | 
			
		||||
            writer.writerow(row)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user