Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Splitting symbols from a file

I’m fairly new to Python, I’ve wrote a function that reads a file and that strips odd symbols out of the file.

def clean_string(x):
    return x.strip().strip('"').strip("'").lower()

and to read the file

with open(fileName, 'r', errors='replace', encoding='utf-8') as f:
        data = f.readlines()
    urls = []
    for line in data:
        cleanName = clean_string(line.split(',')[0])
        cleanServer = clean_string(line.split(',')[1])
        name = request.quote(cleanName.encode('utf-8'))
        server = request.quote(cleanServer.encode('utf-8'))
        url = baseUrl.replace('<name>', name).replace('<server>', server)
        urls.append(url)

While cleanName works perfectly, I’m getting list index out of range for cleanServer, when I print the line with [1] index, it also prints the output, which means the index exists, what am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example data:

"Bicmex"
,"ravencrest"
"Fleshcraft"
,"archimonde"
"Satanå"
,"stormscale"
"Jàgër"

>Solution :

Your data contains a line without server. You need to check for that and f.e. print that line:

Fix:

with open(fileName, 'r', errors='replace', encoding='utf-8') as f:
        data = f.readlines()
    urls = []
    for line in data:
        splits = line.split(',', 1)  # maximal 2 parts, no need to split twice
        cleanName = clean_string(splits[0])
        name = request.quote(cleanName.encode('utf-8'))
        if len(splits) == 2:
             cleanServer = clean_string(splits[1])
             server = request.quote(cleanServer.encode('utf-8'))
             url = baseUrl.replace('<name>', name).replace('<server>', server)
             urls.append(url)
        else:
             print(f"No Server given: '{line}'")

This would work for files like:

"Bicmex","ravencrest"
"Fleshcraft","archimonde"
"Satanå","stormscale"
"Jàgër"

To handle files that have alternating lines, use:

with open(fileName, 'r', errors='replace', encoding='utf-8') as f:
        data = f.readlines()

data = ''.join( zip(data[::2], data [1::2])  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading