I'm new to python and no prior experience at all.
I have a requirement to convert a file contains a single line as: abc,def,ghi,jkl, to another file which should have the values from the above line as below:
abc
def
ghi
jkl
I've tried as below but receives error say that TypeError: can only concatenate list (not "str") to list :
#!/usr/bin/python
inputFile = 'servers'
outputFile = 'cservers'
serverList = []
with open(inputFile) as fi, open(outputFile,'w') as fo:
line = fi.readline()
serverList.append(line.split(','))
for i in serverList:
fo.write(i+'\n')
What is wrong with above code?
Thanks
iis a list of lists. If you want it to be a list of strings, probably doserverList.extend(line.split(',')). Think about what happens ifsplitreturns a list. (Hint: it always does.)