Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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
Python 如何将我的控制台中的whats print()写入.txt文件_Python - Fatal编程技术网

Python 如何将我的控制台中的whats print()写入.txt文件

Python 如何将我的控制台中的whats print()写入.txt文件,python,Python,我对python很在行。这段代码打印到控制台的内容正是我想要输入到.txt文件中的内容。我试过几件事都没有成功。由于我似乎无法附加我正在使用的CSV文件,建议会很有帮助 足球运动员.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

我对python很在行。这段代码打印到控制台的内容正是我想要输入到.txt文件中的内容。我试过几件事都没有成功。由于我似乎无法附加我正在使用的CSV文件,建议会很有帮助

足球运动员.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
  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``
脚本

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
  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``
如何将我的控制台中的whats print()写入.txt文件

您也可以通过更改
sys.stdout
重定向
stdout
,因为
print
始终默认打印到
sys.stdout
。只需在顶部添加这一行:

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.

如果需要,您可以先保存原始的
sys.stdout
,这样您可以在完成后将其更改回来。

如果您在终端控制台中运行此操作,您可以只运行python programName.py>textfile.txt。实际输出到stdout是一件好事。然后,您可以使用shell重定向输出,或者使用
grep
对其进行过滤。尝试:
pythonscript.py>output.csv
pythonscript.py | grep Dragons>output.csv
。您还应该将输入csv文件作为参数。它会更灵活。@Robbie我需要它成为实际代码的一部分。如果有人运行代码,它会自动创建一个.txt文件。你能给出几行csv文件吗?@Robbie当然,我只是把它写进了问题中