To output a text file

Asked 1 years ago, Updated 1 years ago, 122 views

I worked with Python to print the ALE.txt file in the picture like output.txt, but it's blocked to some extent. Can you help me? I made it roughly, but I don't know what to do from here.I don't know if that's right.

infile1=open("ALE.txt","r")

infile2=open("output.txt","w")

str1="Team\t\twon\t\tlost\t\tpercentage"

infile2.write(str1)

for line in infile1:

    list=line.split(",")

python txt csv output

2022-09-21 16:03

3 Answers

No matter how hard I try, I can only print it out like this Is there a better way? I'd appreciate it if you could help me.!


2022-09-21 16:03

Is it hard to get a percentage?

round(won/(won+lost), 3)

You can do it.

Is the next song called "sort"?

In [1]: L = [('a', 3), ('b', 2), ('c', 1)]                                      

In [2]: from operator import itemgetter                                         

In [3]: L.sort(key=itemgetter(1))                                               

In [4]: L                                                                       
Out[8]: [('c', 1), ('b', 2), ('a', 3)]


2022-09-21 16:03

Please refer to the code below Please code it simpler and share it with me...

f = open('c:/Users/USER/Desktop/ALE.txt','r')
s = f.read().split('\n')
f.close()

str1="Team        won         lost        percentage\n"

f2 = open('c:/Users/USER/Desktop/output.txt','w+')
f2.write(str1)

for i in s:
    i = i.split(',')
    i.append('%0.3f' %(int(i[1])/(int(i[1])+int(i[2]))))
    for j in range(len(i)):
        i[j] = i[j] + ' '*(12-len(i[j]))
        f2.write(i[j])
    f2.write('\n')

f2.seek(0)
for k in f2:
    print(k)

f2.close() 

>>> ==================== RESTART: C:/Users/USER/Desktop/b.py =============
Team        won         lost        percentage

Baltimore   96          66          0.593       

Boston      71          91          0.438       

New York    84          78          0.519       

Tampa Bay   77          85          0.475       

Toronto     83          79          0.512  


2022-09-21 16:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.