简体   繁体   中英

how to write whats print() to my console to a .txt file

I'm terrible at python. What this code prints to the console is exactly what I want put into a .txt file. I've tried several things unsuccessfully. Since I can't seem to attach the CSV file I was using, advice would be helpful.

soccer_players.csv :

This is actually the entire CSV file:
Name,Height (inches),Soccer Experience,Guardian Name(s)
Joe Smith,42,YES,Jim and Jan Smith
Jill Tanner,36,YES,Clara Tanner
Bill Bon,43,YES,Sara and Jenny Bon
Eva Gordon,45,NO,Wendy and Mike Gordon
Matt Gill,40,NO,Charles and Sylvia Gill
Kimmy Stein,41,NO,Bill and Hillary Stein
Sammy Adams,45,NO,Jeff Adams
Karl Saygan,42,YES,Heather Bledsoe
Suzane Greenberg,44,YES,Henrietta Dumas
Sal Dali,41,NO,Gala Dali
Joe Kavalier,39,NO,Sam and Elaine Kavalier
Ben Finkelstein,44,NO,Aaron and Jill Finkelstein
Diego Soto,41,YES,Robin and Sarika Soto
Chloe Alaska,47,NO,David and Jamie Alaska
Arnold Willis,43,NO,Claire Willis
Phillip Helm,44,YES,Thomas Helm and Eva Jones
Les Clay,42,YES,Wynonna Brown
Herschel Krustofski,45,YES,Hyman and Rachel Krustofski

script :

  import csv

  #open up the soccer_players.csv and turn the info into a big list with lists inside
  if __name__ == "__main__":
    with open('soccer_players.csv', newline='') as csvfile: 
      artreader = csv.reader(csvfile, delimiter=',')
      rows = list(artreader)

    header = rows.pop(0) #seperating the keys ex: "Name" from the values ex: "John"

    for playerInfo in rows:
      del playerInfo[1] #deleting the height from the lists as we don't need it.

    experience = [] #a list of players with soccer experience
    noExperience = [] #a list of players with no soccer experience

    #putting the players into either the experience or noExperience lists
    for row in rows:
      if row[1] == "YES":
        experience.append(row)
      else:
        noExperience.append(row) 

    teamSharks = []
    teamDragons = []
    teamRaptors = []
    teams = [teamSharks, teamDragons, teamRaptors]
    teamName = ["Sharks", "Dragons", "Raptors"]

    #function combining 1/3 of the experienced/inexperienced into one of the three teams
    def sortingIfPlayed(ifPlayed):
      teamSharks.extend(ifPlayed[:int(len(ifPlayed)/3)])
      teamDragons.extend(ifPlayed[int(len(ifPlayed)/3):int(len(ifPlayed)/(3/2))])
      teamRaptors.extend(ifPlayed[int(len(ifPlayed)/(3/2)):])

    sortingIfPlayed(experience)
    sortingIfPlayed(noExperience)

    #a function to take a list of players info by team, turn it into a string, and print it out 
    def printTeam(team):
      for playerInfoList in team:
        playerInfoString = ", ".join(playerInfoList)

        print(playerInfoString)
      print("")

    x = 0
    for team in teams:
      print(teamName[x])  #prints the team's name above the list of players info
      x += 1
      printTeam(team) #calls the function to print the players info below the team name``

how to write whats print() to my console to a .txt file

you can also just redirect stdout by changing sys.stdout since print always defaults to print to sys.stdout . Just add this line on the top:

import sys
sys.stdout = open('yourtextfile.txt', 'w') # if yourtextfile.txt does not exist, it will create one, if it does, it will override it.

If you want, you can first save the original sys.stdout so you can change it back when you're finished with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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