Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Python CSV将写入字符导出到新行_List_Csv_Python 3.x_Export To Csv - Fatal编程技术网

List Python CSV将写入字符导出到新行

List Python CSV将写入字符导出到新行,list,csv,python-3.x,export-to-csv,List,Csv,Python 3.x,Export To Csv,我一直在使用多个代码片段创建一个解决方案,该解决方案允许我将足球队中的球员列表写入csv文件 import csv data = [] string = input("Team Name: ") fName = string.replace(' ', '') + ".csv" print("When you have entered all the players, press enter.") # while loop that will continue allowing enteri

我一直在使用多个代码片段创建一个解决方案,该解决方案允许我将足球队中的球员列表写入csv文件

import csv

data = []
string = input("Team Name: ")
fName = string.replace(' ', '') + ".csv"

print("When you have entered all the players, press enter.")

# while loop that will continue allowing entering of players
done = False
while not done:
    a = input("Name of player: ")
    if a == "":
        done = True
    else:
        string += a + ','
        string += input("Age: ") + ','
        string += input("Position: ")

print (string)

file = open(fName, 'w')
output = csv.writer(file)

for row in string:
    tempRow = row
    output.writerow(tempRow)

file.close()

print("Team written to file.")
我希望导出的csv文件如下所示:

player1,25,striker
player2,27,midfielder
p
l
a
y
e
r
,
2
5
done = False
strings_list = []
while not done:
    string = ""
    a = input("Name of player: ")
    if a == "":
        done = True
    else:
        string += a + ','
        string += input("Age: ") + ','
        string += input("Position: ") + '\n'
        strings_list.append(string)
等等。但是,当我检查导出的csv文件时,它看起来更像这样:

player1,25,striker
player2,27,midfielder
p
l
a
y
e
r
,
2
5
done = False
strings_list = []
while not done:
    string = ""
    a = input("Name of player: ")
    if a == "":
        done = True
    else:
        string += a + ','
        string += input("Age: ") + ','
        string += input("Position: ") + '\n'
        strings_list.append(string)
等等

有人知道我错在哪里吗

非常感谢
卡尔

你的
字符串
是一个字符串。它不是字符串列表。执行此操作时,您希望它是一个字符串列表:

for row in string:
当您迭代一个字符串时,您就是在迭代它的字符。这就是为什么你每行看到一个字符

声明字符串列表。并将每个字符串附加到它,如下所示:

player1,25,striker
player2,27,midfielder
p
l
a
y
e
r
,
2
5
done = False
strings_list = []
while not done:
    string = ""
    a = input("Name of player: ")
    if a == "":
        done = True
    else:
        string += a + ','
        string += input("Age: ") + ','
        string += input("Position: ") + '\n'
        strings_list.append(string)
现在迭代这个
strings\u列表
,并打印到输出文件。由于您自己将分隔符(逗号)放入字符串中,因此不需要csv编写器

a_file = open(fName, 'w')
for row in strings_list:
    print(row)
    a_file.write(row)
a_file.close()
注意:
string
是Python中标准模块的名称。明智的做法是不要将其用作程序中任何变量的名称。变量
文件也是如此

写入CSV文件时不需要
tempRow
。此外,您应该使用
换行符=''
参数打开文件(--您还可以在这里找到解释)。在你的情况下这可能不重要,但有一天它可能会重要。。。所以,要习惯:)谢谢你的帮助!我已经对它们进行了更改,现在效果好多了。尽管输出文件现在显示
p、l、a、y、e、r、1、2、5、s、t、r、i、k、e、r、
。是否有任何方法对其进行更正,使其按预期显示?Thanks@karldou检查我的最新答案。我已经摆脱了CSV write,并在“string+=input”(“Position:”)+'\n'`行添加了一个新行字符,非常棒!非常感谢,它现在工作得很好!真的很感激!