简体   繁体   English

排序文本文件后打印到新行

[英]Printing to a new line after sorting a text file

For my task I have to be able to print the data that is in text file after it has been sorted. 对于我的任务,我必须能够对已排序的文本文件中的数据进行打印。 I have been able to sort it but it dosen't print it to a new line even though in notepad they are on seperate lines. 我已经能够对其进行排序,但是即使在记事本中它们位于单独的行中,它也不会将其打印到新行中。

My Notepad Documents has this: http://i.stack.imgur.com/Xn0pT.png 我的记事本文档具有以下内容: http : //i.stack.imgur.com/Xn0pT.png

The code I already have set up is: 我已经设置的代码是:

file = open(class_name , 'a')   #opens the file in 'append' mode so you don't delete all the information
name = (name)
file.write(str(name + " : " )) #writes the information to the file
file.write(str(score))
file.write('\n')
file.close()    #safely closes the file to save the information

viewscore = input("Do you wish to view previous results for your class").lower()

if viewscore == "yes".lower():
   f = open(class_name , "r")
   lines = [line for line in f if line.strip()]
   f.close()
   lines.sort()
   print (lines)

The Variables I have are: 我拥有的变量是:

class_name = class_name + ".txt"  
name = input().title()

Then when run the output I get is: 然后,当运行输出时,我得到的是:

['Dan : 0\n', 'Jana : 0\n', 'Kyle : 0\n']

Please tell me if I have to add anything. 请告诉我是否必须添加任何内容。

Here is a work version for me: 这是适合我的工作版本:

class_name = 'data.txt'
name = 'Jim'
score = 100
file = open(class_name , 'a')   #opens the file in 'append' mode so you don't delete all the information

line_data = name + " : " + str(score) + "\n" # data to write
file.write(line_data)
file.close()    #safely closes the file to save the information

viewscore = raw_input("Do you wish to view previous results for your class?").lower()

if viewscore == "yes".lower():
   f = open(class_name , "r")
   lines = [line for line in f if line.strip()]
   f.close()
   lines.sort()
   for line in lines:
      print line
else: # add else case to debug
   print 'no for', viewscore

First, you can put the line you want to write in a variable, and then write it. 首先,可以将要编写的行放入变量中,然后再编写。

Second, if you use Python2.x, use raw_input() for input string. 其次,如果您使用Python2.x,请使用raw_input()作为输入字符串。

Third, if you have a if , better to write an else for easy to debug the code. 第三,如果您具有if ,则最好编写一个else以便于调试代码。

You should try this code. 您应该尝试此代码。 Why are you writing 3 things separately? 你为什么要分别写三件事? It is probably writing \\n as a string, not putting things in new line. 它可能是将\\ n写为字符串,而不是将其放在换行符中。

f.write(name+' : '+str(score)+'\n')

What you are printing is the list you called lines and the way you see the output is the way the type list is displayed. 您要打印的是称为lines的列表,而查看输出的方式就是显示类型列表的方式。 What you want to do is print the content of the list as one string. 您要做的是将列表的内容打印为一个字符串。 For that you can join the content as follow: 为此,您可以按以下方式join内容:

In [1]: lines = ['Dan : 0\n', 'Jana : 0\n', 'Kyle : 0\n']
In [2]: print "".join(lines)
Dan : 0
Jana : 0
Kyle : 0

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

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