0

I have a text file (target file of NWPU VHR-10 dataset) that included 2 tuples and one integer for each line e.g each line has the text like this:

(563,478),(630,573),1

Now I wanna find out how to split and read it like two tuples and one integer. e.g:

tuple 1: (563,478) tuple 2: (630,573) int: 1

thanks

  • 1
    It's not clear what is your question, Do you want to split the read in `lines` to what kind of format? Can you show what's you got then? – Daniel Hao Jan 02 '21 at 17:39
  • Have you tried to split one line at commas, strip the parentheses from each part, convert the results to integers, and combine the first and second, and the third and fourth integer to tuples, and then repeat this for every line? – mkrieger1 Jan 02 '21 at 17:43
  • 2
    Try adaping some solutions from here: https://stackoverflow.com/questions/48094176/read-tuples-from-text-file?rq=1 – Nyps Jan 02 '21 at 17:44
  • @DanielHao I wanna all formats to be integers in tuples. 2 tuples consist of 2 integers and an integer. This is after I read the lines in the file. – mohammad javad taheri Jan 02 '21 at 17:52

1 Answers1

0

thanks for @mkrieger1 the solution I found was this

    import re

    for line in target_file:
        line = re.sub(r"([()])", "", line)
        lst = list(map(int, line.split(',')))
        k, t, p = (lst[0], lst[1]), (lst[2], lst[3]), lst[4]
        print(k)
        print(t)
        print(p)