Hardik
Hardik

Reputation: 25

Want to delete quotes (" ") from the end of csv file


I am reading csv file in Python. But it gives double quotes at the end of every line which I don't want.

My code

with open('data.csv', 'r') as file:
reader = csv.reader(file)
for line in reader:
    lines = line[2:]
    lines1 = ' '.join(lines).split(',')

    for row in lines1[::]:
        makeOneLine = row.rstrip('\n')
        #print(single_row)
        delimiter = " "
        makeOneLine = delimiter.join(makeOneLine.split(delimiter, 3)[3:])[:-2]
        numpyArraysDisplay = np.array(list(makeOneLine.split(" ")))
        print(numpyArraysDisplay)
        outputfile.write(str(numpyArraysDisplay) + '\n')

        for x in range(0, numpyArraysDisplay.size-1):
            part1 = numpyArraysDisplay[:x + 1:]
            strings, counts = np.unique(part1, return_counts=True)
            CountWordsfrequency = np.array(list(dict(zip(strings, counts)).values()))
            print(CountWordsfrequency)
            outputfile.write(str(CountWordsfrequency) + '\n')

I am getting output after reading file as per below:

['q1' 'i3' 'q1' 'i3' 'q1' 'i3' 'q1' 'i3' 'dv' 'q1' 'i3' 'q1' 'i3' 'c1'
 'i3' 'dv' 'dv' 'q1' 'i3' 'c1' 'i3' 'c1' 'i3' 'c1' 'i3' 'q1' 'i3' 'c1'
 'i3' 'q7' 'i3' 'c1' 'i3' 'q1' 'i3' 'c1' 'i3' 'c3' 'i3' 'c7' 'i3' 'c7'
 'i3' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''
 '' '' '' '' '' '' '' '' '' '' '' '' '' '']

I don't want the empty quotes.

Upvotes: 1

Views: 47

Answers (1)

ThePyGuy
ThePyGuy

Reputation: 1035

list(filter(None, CountWordsfrequency))

Upvotes: 1

Related Questions