Python Forum
How I can multiply the fourth one from each row in the text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How I can multiply the fourth one from each row in the text file
#3
As philia states, it's unclear as to what your intention is, however to access the various elements of your file,
split each line into a list, then it will be easy to work with. Example:
import os

# make sure we are in same directory as script:
os.chdir(os.path.abspath(os.path.dirname(__file__)))


with open('fin.txt', 'r') as fp:
    linecount = 1
    for line in fp:
        line = line.strip().split(',')
        print()
        for n, item in enumerate(line):
            print('item {} of line {} is equal to {}'.format(n, linecount, item))
        linecount += 1
will generate:
Output:
item 0 of line 1 is equal to - 2 item 1 of line 1 is equal to 1 item 2 of line 1 is equal to 2 item 3 of line 1 is equal to 3 item 4 of line 1 is equal to **4** item 5 of line 1 is equal to 15 item 6 of line 1 is equal to 14 item 7 of line 1 is equal to 29 item 8 of line 1 is equal to 64168.0 item 9 of line 1 is equal to 1 item 0 of line 2 is equal to 2 item 1 of line 2 is equal to 1 item 2 of line 2 is equal to 2 item 3 of line 2 is equal to 3 item 4 of line 2 is equal to **6** item 5 of line 2 is equal to 15 item 6 of line 2 is equal to 14 item 7 of line 2 is equal to 29 item 8 of line 2 is equal to 64168.0 item 9 of line 2 is equal to 1 item 0 of line 3 is equal to 2 item 1 of line 3 is equal to 1 item 2 of line 3 is equal to 2 item 3 of line 3 is equal to 3 item 4 of line 3 is equal to **8** item 5 of line 3 is equal to 15 item 6 of line 3 is equal to 14 item 7 of line 3 is equal to 29 item 8 of line 3 is equal to 64168.0 item 9 of line 3 is equal to 1
Reply


Messages In This Thread
RE: How I can multiply the fourth one from each row in the text file - by Larz60+ - Dec-25-2018, 03:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Python 2.7] Why can't I press [ESC] a fourth time? Ashwood 3 1,669 Aug-27-2023, 02:01 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 17,968 Jan-21-2023, 06:57 AM
Last Post: jacklee26
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,164 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,346 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 11,547 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 5,127 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 7,539 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 3,380 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 3,037 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 26,295 Jul-17-2020, 01:45 PM
Last Post: marvel_plato

Forum Jump:

User Panel Messages

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