G
G
Gast0n2020-05-15 17:56:59
Python
Gast0n, 2020-05-15 17:56:59

How to check and remove empty lines?

This script checks certain columns in csv and outputs their value to a file. How to make a pass if there was no value, but now it displays empty lines. Can as that on length of value to check?

import csv
 
def csv_reader(file_obj):
    """
    Read a csv file
    """
    reader = csv.DictReader(file_obj, delimiter=';')
    for line in reader:
        r = (line["Рабочий email"])  
        l = (line["Личный email"])

        e = r.split(',')
        d = l.split(',')

        base = e + d
        
        
        with open('data.txt', "a") as file:
            for line in base:              
                file.write(line + '\n')    

                
if __name__ == "__main__":
    csv_path = "1.csv"
    with open(csv_path, "r", encoding='utf-8') as f_obj:
        csv_reader(f_obj)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-05-15
@Gast0n

under

r = (line["Рабочий email"])  
l = (line["Личный email"])

add something like this:
if not r.strip() and not l.strip():
     continue

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question