list - Python CSV export writing characters to new lines -
I am using multiple code snippets to create a solution that allows me to write a list of players in a football team Will give a CSV file.
import csv data = [] string = input ("team name:") fName = string.replace ('', '') + ".csv" print ("when you all Players have entered, then hit enter.) # While the loop will continue to enter the players, when it will not be done: a = input ("player name:") if any == "": true Second: String + = A + ',' String + = Input ("Age:") + ',' String + = Input ("Status:") Print (string) file = Open (Fnm, 'W') Output = Csv.writer (file) for the row in the row: tempRow = line output.writerow (tempRow) file.close () print ("Team written in file.") me Like to export CSV file to appear like this:
player 1, 25, striker player 2, 27, midfielder and so on.However, When I check the Export CSV file, it looks like this:
player, 2 5 and so on. < p> Does anyone know that I am wrong
your code> is a single string This is not a list of strings when you are doing this, you are expecting to be a list of strings:
for line in line: < / code> When you iterate over a string, you are running on your characters, which is why you are seeing one letter per line.
Announce a list of wires. And add it like every string:
done = false string_list = [] It has not been done: string = "" A = input ("player name:") If any = = "" String = = input ("position:") + '\ n' strings_list.append (string): string + = A + ',' string + = input ("age:") Now repeat strings_list on it and print it on the output file. Since you are putting delimiter (comma) in the string, you do not need a CSV writer. a_file = open (fName, 'w') for the line in string_list: print (line) A_file.write (line) a_file.close () < p> Note: string is the name of a standard module in Python. It is not wise to use it as the name of any variable in your program. The same goes for your variable file
Comments
Post a Comment