How to remove or ignore control character \r or " " from cells in excel sheet?

I am reading a csv file data and converting them into dictionary but the data in dictionary is coming with \r like 69\r, 79\r in putty or When I open the file, it is coming as "69" and these double quotes are hidden. How to remove these?

I tried many ways from stack overflow but didn't work out. I tried read.splitLines() or replace method as well but problem with that is if I remove \r then it will remove all the data after 1st line but that excel has lot of data.

Is there any other way where we can ignore control characters and get all the data in excel or putty. Please help!!!!!

Below is the code:

def fileToDict(filepath):
    dataInDict = {}
    with open(filepath) as f:
        data = f.readlines()
        for idx, d in enumerate(data):
            dlist = d.strip('\n').split('\t')
            dataInDict[idx] = dlist
        f.close()
    return dataInDict 

Adding more details here... I am using this function to convert all the data from 2 files into 2 dictionaries using like

Dict1 =  fileToDict(file1)  
Dict2 =  fileToDict(file2) 

Now when I am comparing data between two dictionaries and printing dataInDict using above function, it gives me output with data from both dictionaries as shown below. I have highlighted the \r values and without \r values. enter image description here

but when I try to remove \r from this, it gives me only 1st line and doesn't give all the lines. Because of this \r, comparison is failing

I am writing above output to excel sheet using xlsWriter

Upvotes: 0

Views: 708

Answers (1)

dotchetter
dotchetter

Reputation: 45

There's a couple of ways to solve this.

You could use the .strip method in a for loop.

Presume you have this code:

mydict = {'one': '69\r', 'two': '79\r'}

You could iterate over it and replace each value in every key with a stripped version of itself:

for i in mydict:
    mydict[i] = mydict[i].strip('\r')

Output:

{'one': '69', 'two': '79'}

EDIT

Since the author added a code snippet:

def fileToDict(filepath):
    dataInDict = {}
    with open(filepath) as f:
        data = f.readlines()
        for idx, d in enumerate(data):
            dlist = d.strip('\n').split('\t')
            dataInDict[idx] = dlist
        f.close()
    for i in dataInDict:                           # Iterate through your new dict
        dataInDict[i] = dataInDict[i].strip('\r')  # Strip each value after occurance of '\r'
    return dataInDict 

Upvotes: 1

Related Questions