i am performing a hive query and storing the output in a tsv file in the local FS. I am running a for loop for the hive query and passing different parameters. If the hive query returns no output once in the for loop it prints an empty line in the tsv file. This causes NULL values to be pushed to my DB in the backend. Hence, after the for loop runs and the file is created - i have the below code to remove all the empty lines printed, but it doesn't work.
How do i remove the empty line from this file?
` 395.9 429.61 PT
`
code:
with open('file.tsv','r+w') as file:
for line in file:
if line.strip():
file.write(line)
thanks
Usually you would open the input file and write the non-empty lines to a second file:
If you want to filter the file inplace you can use
FileInput
with theinplace
option:however, this uses an intermediate file and may not work in low disk space situations.
To filter the file inplace without allocating any additional disk space you could try something like this: