0

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

2
  • 3
    i is a list of lists. If you want it to be a list of strings, probably do serverList.extend(line.split(',')). Think about what happens if split returns a list. (Hint: it always does.) Commented Oct 2, 2020 at 8:39
  • Understood now. Thank you Commented Oct 2, 2020 at 13:30

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

This will only handle a single line of input, and overwrite the serverList on each iteration over the file.
I know, but he only has one line anyway. I added an edit to clarify that, thanks
Yes, its clear now. Currently there is only one line. Thank you for the explanation!!
1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.