简体   繁体   English

来自csv行的Python列表并将列表写入文件

[英]Python list from csv line and write list to file

I'm new to python and no prior experience at all.我是 python 的新手,根本没有任何经验。
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 ,转换为另一个文件,该文件应具有上一行中的值,如下所示:

abc
def
ghi
jkl

I've tried as below but receives error say that TypeError: can only concatenate list (not "str") to list :我试过如下但收到错误说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谢谢

split returns a list, not a string. split返回一个列表,而不是一个字符串。 After the line serverList.append(..) , the variable serverList is a list conataining a list that contains strings, looks something like that: [['abc', 'def', ...]].serverList.append(..)行之后,变量serverList是一个包含字符串列表的列表,看起来像这样:[['abc', 'def', ...]]。

So serverList is a list of lists, with one element.所以 serverList 是一个列表列表,只有一个元素。 Then you iterate through serverList (remember, there is only one element) and the element is a list.然后你遍历 serverList(记住,只有一个元素),这个元素是一个列表。 You try to add '\\n' to a list, which is an error: TypeError: can only concatenate list (not "str") to list .您尝试将 '\\n' 添加到列表中,这是一个错误: 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然后整个列表返回的split命令将在 serverList 中的一个元素内

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如果以后要遍历行,可以使用serverList = serverList + line.split(',')将两个列表相加

In your example the line在您的示例中,该行

    serverList.append(line.split(','))

results in a serverList that looks like [['abc', 'def', 'ghi', 'jkl']] .导致serverList看起来像[['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.由于控制台输出中给出的原因,您将收到TypeError抛出。

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.您可以通过更改代码来修复代码,使serverList是一个字符串列表,表示要写入outputFile ,而不是包含字符串列表的列表。 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

When you use the split() function it returns a list当您使用 split() 函数时,它会返回一个列表

So your list named serverList now contains another list.因此,名为 serverList 的列表现在包含另一个列表。

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.意味着您正在遍历 serverList 并记住此列表的每个元素都是另一个列表。

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.您正在尝试使用不可能的字符串连接(加入)列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM