Your output doesn't make sense if you're generating csv.
That said, string concatenation inside a loop is normally frowned upon for performance reasons, and lists are suggested instead. In your case, you also get the added benefit of removing the trailing pipe that way.
Here's some completely untested code, which should be pretty close to right:
That said, string concatenation inside a loop is normally frowned upon for performance reasons, and lists are suggested instead. In your case, you also get the added benefit of removing the trailing pipe that way.
Here's some completely untested code, which should be pretty close to right:
with open(inFile) as f: with open(outFile, "w") as fout: parts = [ ] for line in f: line = line.strip() if line: parts.append(line) else: print("|".join(parts), file=fout) parts = [ ] if parts: print("|".join(parts), file=fout)