Python Forum
How I can multiply the fourth one from each row in the text file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How I can multiply the fourth one from each row in the text file (/thread-14930.html)



How I can multiply the fourth one from each row in the text file - canatilaa - Dec-25-2018

- 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`



RE: How I can multiply the fourth one from each row in the text file - Philia - Dec-25-2018

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()



RE: How I can multiply the fourth one from each row in the text file - Larz60+ - Dec-25-2018

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