Reputation: 1208
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
Upvotes: 0
Views: 92
Reputation: 31
When you use the split() function it returns a list
So your list named serverList now contains another list.
Now this code here:
for i in serverList:
fo.write(i+'\n')
Means you're looping through the serverList and remember that each element of this list is another list.
So that is the reason for the error. Because here in your code:
+'\n'
You are trying to concatenate (join) a list with a string which is not possible.
Upvotes: 1
Reputation: 1557
In your example the line
serverList.append(line.split(','))
results in a serverList
that looks like [['abc', 'def', 'ghi', 'jkl']]
. So when you come to execute
fo.write(i+'\n')
You will get a TypeError
thrown for the reason given in the console output.
You can test this by running.
a = [['abc', 'def', 'ghi', 'jkl']]
print(a+'\n')
You could fix your code by changing your code so that serverList
is a list of strings representing lines to be written to outputFile
as opposed to a list containing a list of strings. See below for example solution.
#!/usr/bin/python
inputFile = 'servers'
outputFile = 'cservers'
with open(inputFile) as fi:
serverList = fi.read().split(',') # Read file contents as list of strings delimited by comma in file
with open(outputFile,'w') as fo:
fo.writelines(s + '\n' for s in serverList) # Write each string in serverList on own line
Upvotes: 1
Reputation: 3066
split
returns a list, not a string. After the line serverList.append(..)
, the variable serverList
is a list conataining a list that contains strings, looks something like that: [['abc', 'def', ...]].
So serverList is a list of lists, with one element. Then you iterate through serverList (remember, there is only one element) and the element is a list. You try to add '\n' to a list, which is an error: TypeError: can only concatenate list (not "str") to list
. Generally, use the error you receive to see what wrong: it says you are tried to add string with a list.
To fix it, just use
serverList = line.split(',')
Then the whole list returns for split
commands will be in serverList inside of an element within it
Edit: as mentioned in comments, this will handle only one line of input - but from what I understand, that is okay for you (as you also read only one line). If you want to iterate through lines in the future, you can use serverList = serverList + line.split(',')
which adds two lists together
Upvotes: 1