Python Forum
Doing calculation with ascii file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Doing calculation with ascii file
#5
Yes can do it like this.
Here an other version you can look at using f-string.
Also put in header line just as a example.
See that with string formatting(f-string) it look better no need for str() or +.
with open('col.txt') as f,open('total.txt', 'w') as f_out:
    header = next(f)
    c1, c2 = header.strip().split(',')
    f_out.write(f'{c1} {c2}  Sum\n')
    for line in f:
        line = line.strip()
        num1, num2 = line.split(',')
        num1, num2 = int(num1), int(num2)
        num3 = num1 + num2
        f_out.write(f'{num1} + {num2}  ${num3}\n')
Output:
colA colB Sum 324 + 234 $558 346 + 341 $687 147 + 346 $493 234 + 567 $801 368 + 405 $773 344 + 643 $987 235 + 235 $470 236 + 567 $803
Reply


Messages In This Thread
Doing calculation with ascii file - by Mike - Oct-02-2019, 11:09 AM
RE: Doing calculation with ascii file - by snippsat - Oct-02-2019, 11:42 AM
RE: Doing calculation with ascii file - by Mike - Oct-02-2019, 12:17 PM
RE: Doing calculation with ascii file - by Mike - Oct-02-2019, 01:34 PM
RE: Doing calculation with ascii file - by snippsat - Oct-02-2019, 01:55 PM
RE: Doing calculation with ascii file - by Mike - Oct-02-2019, 02:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read ASCII File pyth0nus3r 3 8,380 May-23-2019, 11:13 PM
Last Post: michalmonday
  need algorithm to strip non-ascii characters from LONG csv file hereathome 3 3,764 Jan-12-2018, 02:04 AM
Last Post: hereathome

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020