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?
Example data:
"Bicmex"
,"ravencrest"
"Fleshcraft"
,"archimonde"
"Satanå"
,"stormscale"
"Jàgër"
CodePudding user response:
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])