Home > Net >  How to add in N/A in csv file when there is no data in the file?
How to add in N/A in csv file when there is no data in the file?

Time:10-13

For now in the csv file, I wish to add in N/A when the file have no "Step" or no "data" in a new line just like the picture shown and I do not know how to proceed with the code.

enter image description here

for eachline in file:
            if "Step" in eachline:
                step_list = eachline.strip().split()
                step_matches = [matches for matches in step_list if "Step" in matches]
                variable = list(step_matches)
                new_list = "".join(variable).replace(",","|")
                print(new_list)
                flag = True
            if flag:
                if "data1" in eachline:
                    folder.write(eachline.strip())
                    folder.write(next(file).strip())
                    folder.write(next(file).strip(),"\n")

CodePudding user response:

I would be mapping out a copy of the CSV file in memory using nested arrays or an array of data structures depending on if you know how many columns or rows there will be and checking every piece of data to see if it's empty. If not chuck it in the copy I'm making in memory and if so store "N/A" in it's place.

CodePudding user response:

If you want to replace 'N/A' values in a data frame you could use pandas replace do this:

df = df.replace('N/A', np.nan)
  • Related