Remove ''\n" from list of strings while file reading in Python

Viewed 320
def files_to_dict(folder_name):
    list_of_files = os.listdir("./"+folder_name) #read file names of current dir in list
    newDict=dict()
    for year in (list_of_files):
        if(year!=".ipynb_checkpoints"):
            ofile = open("./"+folder_name+"/"+year,"r")
            data = ofile.read().split(',')

    return data

I am trying to remove all delimiters while reading file into a list, including '\n'. I have tried using the above method but it gives the output like

'Emma', 'F', '20799\nOlivia', 'F', '19674\nSophia', 'F', '18490\nIsabella', 'F', '16950\nAva',

The list goes on will the same pattern. I want to remove the '\n' from the middle of the string in a list. I want to find an efficient solution which doesn't involve running a loop on the entire list again and removing the '\n' from each index.

Expexted Output:

'Emma', 'F', '20799', 'Olivia', 'F', '19674', 'Sophia', 'F', '18490', 'Isabella', 'F', '16950', 'Ava',

5 Answers

I think you are trying to replace the "\n" characters with a delimiter rather than removing them:

def files_to_dict(folder_name):
    list_of_files = os.listdir("./"+folder_name) #read file names of current dir in list
    newDict=dict()
    for year in (list_of_files):
        if(year!=".ipynb_checkpoints"):
            with open("./"+folder_name+"/"+year,"r") as ofile:
                data = ofile.read().replace('\n', ',').split(',')
                return data

I don't know what your files look like, but I see you're never using newDict so you just return the last file what was processed

Try seeing if the following is closer to what you want

with open("./"+folder_name+"/"+year) as ofile:
    data_lines = [s.rstrip() for s in ofile.readlines()] 
    # would be better if you used csv module 
    data = [s.split(',') for s in data_lines]
    print(data)

It's very simple ,use split() instead of split(',') in your code.I modified your code and it is shown below:-

def files_to_dict(folder_name):
list_of_files = os.listdir("./"+folder_name) #read file names of current dir in list
newDict=dict()
for year in (list_of_files):
    if(year!=".ipynb_checkpoints"):
        ofile = open("./"+folder_name+"/"+year,"r")
        data = ofile.read().split()

return data

Please refer to the following code and output if any more confusion is there.To understand easily i executed the below code based on your input

code:

fh=open("trystack.txt",'r')
for line in fh:
    lines=fh.read().split()
    print(lines)  
fh.close() 

output:

['Olivia', 'F', '19674', 'Sophia', 'F', '18490', 'Isabella', 'F', '16950']

My text file trystack.txt contains:

Emma F 20799

Olivia F 19674

Sophia F 18490

Isabella F 16950

This will help you in achieving what you need i.e removing '\n'

ThankYou

Use data.strip() to remove ā€˜\n’ from a string

data =  'Emma', 'F', '20799\nOlivia', 'F', '19674\nSophia', 'F', '18490\nIsabella', 'F', '16950\nAva','Emma', 'F', '20799\nOlivia', 'F', '19674\nSophia', 'F', '18490\nIsabella', 'F', '16950\nAva'
sum((s.split('\n') for s in data), [])

['Emma', 'F', '20799', 'Olivia', 'F', '19674', 'Sophia', 'F', '18490', 'Isabella', 'F', '16950', 'Ava', 'Emma', 'F', '20799', 'Olivia', 'F', '19674', 'Sophia', 'F', '18490', 'Isabella', 'F', '16950', 'Ava']

or if it is a string like this below:

data = "'Emma', 'F', '20799\nOlivia', 'F', '19674\nSophia', 'F', '18490\nIsabella', 'F', '16950\nAva','Emma', 'F', '20799\nOlivia', 'F', '19674\nSophia', 'F', '18490\nIsabella', 'F', '16950\nAva'"

import re
re.findall(r"[\w]+", data)

['Emma', 'F', '20799', 'Olivia', 'F', '19674', 'Sophia', 'F', '18490', 'Isabella', 'F', '16950', 'Ava', 'Emma', 'F', '20799', 'Olivia', 'F', '19674', 'Sophia', 'F', '18490', 'Isabella', 'F', '16950', 'Ava']

Related