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
#1
- 2, 1, 2, 3, **4**, 15, 14, 29, 64168.0, 1

2, 1, 2, 3, **6**, 15, 14, 29, 64168.0, 1

2, 1, 2, 3, **8**, 15, 14, 29, 64168.0, 1

I wanna multiply this item








    elif menu == 4:
        fin = open("data.txt", "r")
        i = 0
        n = 0
        freeday = fin.read[i][9]
        i = i + 1

        print freeday
    # 45 saatten fazla calısma yasası.......(n)
    elif menu == 5:
        fin = open("data.txt", "r")
        for line in fin:
            totalweekdayshour = [5]
            totalweekendhour = [6]
        if totalweekdayshour + totalweekendhour >= 45:

            print ("workers must be work 45 h")

        else:
            print ("no problem")
        fin.close`
Reply
#2
Properly format and indent your code. It's much more readable. Not to mention python code will not work without proper indentation.
I formatted and indented your code below. But It's not clear what you are trying to do here.

    elif menu == 4:
        fin = open("data.txt", "r")
        i = 0
        n = 0
        freeday = fin.read[i][9]
        i = i + 1

        print freeday
# 45 saatten fazla calısma yasası.......(n)
    elif menu == 5:
        fin = open("data.txt", "r")
        for line in fin:
            totalweekdayshour = [5]
            totalweekendhour = [6]
            if totalweekdayshour + totalweekendhour >= 45:

               print ("workers must be work 45 h")

    else:
        print ("no problem")
fin.close()
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Python 2.7] Why can't I press [ESC] a fourth time? Ashwood 3 658 Aug-27-2023, 02:01 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,957 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 1,123 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,668 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,970 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,349 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,400 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 2,402 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,136 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 19,695 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