Assign to variables from text file



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have this text:



(empty line)
(empty line)
7 -1 -2
2 -2
(empty line)
-6 2 -5 8
(empty line)
(empty line)
(3, 2), (6,4),
(2,8), (3,4), (0,6),
(6,6), (7,2)
(empty line)
(empty line)


There are occasional empty lines.



What I wrote is



with open(txt,encoding='utf8')as f:
text = f.read().strip()
t = [i.replace(" ", "") for i in text.splitlines() ]


it gives me:



['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']


I would like to assign to 3 different variables:



d1 = [7,-1,-2,2,-2]
d2 = [-6,2,-5,8]
sets = [(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)]









share|improve this question



















  • 1





    Sorry for my English, how can I improve the question

    – LordNord
    Nov 15 '18 at 13:38











  • Your last section "i would like to assign..." is difficult to understand.

    – Ctrl S
    Nov 15 '18 at 13:39






  • 1





    I did correction to be more clear, I wrote information that is confusing, sorry

    – LordNord
    Nov 15 '18 at 13:40











  • Do you want your d1 and d2 to be integers or strings?

    – Babak
    Nov 15 '18 at 14:32












  • I need them as integers, Thanks @Babak

    – LordNord
    Nov 15 '18 at 14:44

















1















I have this text:



(empty line)
(empty line)
7 -1 -2
2 -2
(empty line)
-6 2 -5 8
(empty line)
(empty line)
(3, 2), (6,4),
(2,8), (3,4), (0,6),
(6,6), (7,2)
(empty line)
(empty line)


There are occasional empty lines.



What I wrote is



with open(txt,encoding='utf8')as f:
text = f.read().strip()
t = [i.replace(" ", "") for i in text.splitlines() ]


it gives me:



['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']


I would like to assign to 3 different variables:



d1 = [7,-1,-2,2,-2]
d2 = [-6,2,-5,8]
sets = [(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)]









share|improve this question



















  • 1





    Sorry for my English, how can I improve the question

    – LordNord
    Nov 15 '18 at 13:38











  • Your last section "i would like to assign..." is difficult to understand.

    – Ctrl S
    Nov 15 '18 at 13:39






  • 1





    I did correction to be more clear, I wrote information that is confusing, sorry

    – LordNord
    Nov 15 '18 at 13:40











  • Do you want your d1 and d2 to be integers or strings?

    – Babak
    Nov 15 '18 at 14:32












  • I need them as integers, Thanks @Babak

    – LordNord
    Nov 15 '18 at 14:44













1












1








1








I have this text:



(empty line)
(empty line)
7 -1 -2
2 -2
(empty line)
-6 2 -5 8
(empty line)
(empty line)
(3, 2), (6,4),
(2,8), (3,4), (0,6),
(6,6), (7,2)
(empty line)
(empty line)


There are occasional empty lines.



What I wrote is



with open(txt,encoding='utf8')as f:
text = f.read().strip()
t = [i.replace(" ", "") for i in text.splitlines() ]


it gives me:



['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']


I would like to assign to 3 different variables:



d1 = [7,-1,-2,2,-2]
d2 = [-6,2,-5,8]
sets = [(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)]









share|improve this question
















I have this text:



(empty line)
(empty line)
7 -1 -2
2 -2
(empty line)
-6 2 -5 8
(empty line)
(empty line)
(3, 2), (6,4),
(2,8), (3,4), (0,6),
(6,6), (7,2)
(empty line)
(empty line)


There are occasional empty lines.



What I wrote is



with open(txt,encoding='utf8')as f:
text = f.read().strip()
t = [i.replace(" ", "") for i in text.splitlines() ]


it gives me:



['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']


I would like to assign to 3 different variables:



d1 = [7,-1,-2,2,-2]
d2 = [-6,2,-5,8]
sets = [(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)]






python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:39







LordNord

















asked Nov 15 '18 at 13:13









LordNordLordNord

707




707







  • 1





    Sorry for my English, how can I improve the question

    – LordNord
    Nov 15 '18 at 13:38











  • Your last section "i would like to assign..." is difficult to understand.

    – Ctrl S
    Nov 15 '18 at 13:39






  • 1





    I did correction to be more clear, I wrote information that is confusing, sorry

    – LordNord
    Nov 15 '18 at 13:40











  • Do you want your d1 and d2 to be integers or strings?

    – Babak
    Nov 15 '18 at 14:32












  • I need them as integers, Thanks @Babak

    – LordNord
    Nov 15 '18 at 14:44












  • 1





    Sorry for my English, how can I improve the question

    – LordNord
    Nov 15 '18 at 13:38











  • Your last section "i would like to assign..." is difficult to understand.

    – Ctrl S
    Nov 15 '18 at 13:39






  • 1





    I did correction to be more clear, I wrote information that is confusing, sorry

    – LordNord
    Nov 15 '18 at 13:40











  • Do you want your d1 and d2 to be integers or strings?

    – Babak
    Nov 15 '18 at 14:32












  • I need them as integers, Thanks @Babak

    – LordNord
    Nov 15 '18 at 14:44







1




1





Sorry for my English, how can I improve the question

– LordNord
Nov 15 '18 at 13:38





Sorry for my English, how can I improve the question

– LordNord
Nov 15 '18 at 13:38













Your last section "i would like to assign..." is difficult to understand.

– Ctrl S
Nov 15 '18 at 13:39





Your last section "i would like to assign..." is difficult to understand.

– Ctrl S
Nov 15 '18 at 13:39




1




1





I did correction to be more clear, I wrote information that is confusing, sorry

– LordNord
Nov 15 '18 at 13:40





I did correction to be more clear, I wrote information that is confusing, sorry

– LordNord
Nov 15 '18 at 13:40













Do you want your d1 and d2 to be integers or strings?

– Babak
Nov 15 '18 at 14:32






Do you want your d1 and d2 to be integers or strings?

– Babak
Nov 15 '18 at 14:32














I need them as integers, Thanks @Babak

– LordNord
Nov 15 '18 at 14:44





I need them as integers, Thanks @Babak

– LordNord
Nov 15 '18 at 14:44












4 Answers
4






active

oldest

votes


















2














Adding to what @DocDriven has done:



with open('testt.txt',encoding='utf8')as f:
text = f.read().strip()
t = [i.split(" ") for i in text.splitlines() if len(i)>0 ]

def findTuple(input):
input = "".join(["".join(i) for i in input])
pairs = input.replace("),","|").replace("(","").replace(")","").split("|")
tuples=
for pair in pairs:
tuples.append(tuple(map(int,pair.split(","))))
return tuples

d1 = [list(map(int,i)) for i in t[:2]]
d2 = [int(i) for i in t[2]]
sets = findTuple(t[3:])


will result in:



d1= [[7, -1, -2], [2, -2]]
d2= [-6, 2, -5, 8]
sets= [(3, 2), (6, 4), (2, 8), (3, 4), (0, 6), (6, 6), (7, 2)]





share|improve this answer

























  • Thank you very much ,but I don't need to sum or subtract the d1 and d2, I need to keep them as them are.for example d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8]

    – LordNord
    Nov 15 '18 at 15:38











  • Updated the code, but this is a hack not a program.

    – Babak
    Nov 15 '18 at 22:00


















1














This is not the most concise solution, but since it is dynamic it provides flexibility and can be easily altered to handle new and/or additional data.



# (Already exists)
t = ['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']
# ----------------
dataCount = 3 # adjust this according to how many groups of data are expected (if known)

values = ['' for _ in range(dataCount)]
i = 0
for e in lst:
values[i] = values[i] + e # append the new data to the element
if e == "" and values[i] != "": # advance to new element but ignore repeat "nulls"
i = i + 1

# various ways of displaying the data:
print (values)
for e in values:
print (e)

print (values[0], values[1], values[2])


Output:



['7-1-22-2', '-62-58', '(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)']
7-1-22-2
-62-58
(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)
7-1-22-2 -62-58 (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)


Instead of separate variables, I assigned each group of data to its own list element.






share|improve this answer

























  • Thank you @Ctrl S , I will check soon, thanks for the notes

    – LordNord
    Nov 15 '18 at 14:45











  • If you need d1 and d2 to be integers, what happens to the -?

    – Ctrl S
    Nov 15 '18 at 14:47











  • It's a negative number,after I need to read each integer to do addition or subtraction ,So i think it will not work in this way, because I need to access each number and tuple

    – LordNord
    Nov 15 '18 at 15:08











  • The problem is to separate the whole number to digits, if all were positive it would be easier , you think is there any efficient way to convert it to d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8] even if they are strings, it's not important

    – LordNord
    Nov 15 '18 at 16:38











  • Why do you remove spaces in t = [i.replace(" ", "") for i in text.splitlines() ]? Without the spaces, you no longer know where one number ends and the next one begins. -6 2 turns into -62...

    – Ctrl S
    Nov 15 '18 at 17:36


















0














Try this (notice that all variables are strings, not list of sets):



d1 = "".join(t[:2])
d2 = t[3]
sets = "".join(t[6:])





share|improve this answer























  • The problem comes to other inputs, the thing that helps divide d1,d2 and the sets are the spaces. they help you distinguish. Thank you for the try

    – LordNord
    Nov 15 '18 at 14:13


















0














Thank you all for helping me, big thanks to @Ctrl S ,I solved the problem.



with open(txt,encoding='utf8')as f:
text = f.read().strip()
lists = ['','','']
listindex = 0
for i in text.splitlines():
if i:
lists[listindex] += i
if i == "" and lists[listindex] != "":
listindex += 1
d1,d2,sets = lists


to convert the sets to tuples , I just used the eval().






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320307%2fassign-to-variables-from-text-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Adding to what @DocDriven has done:



    with open('testt.txt',encoding='utf8')as f:
    text = f.read().strip()
    t = [i.split(" ") for i in text.splitlines() if len(i)>0 ]

    def findTuple(input):
    input = "".join(["".join(i) for i in input])
    pairs = input.replace("),","|").replace("(","").replace(")","").split("|")
    tuples=
    for pair in pairs:
    tuples.append(tuple(map(int,pair.split(","))))
    return tuples

    d1 = [list(map(int,i)) for i in t[:2]]
    d2 = [int(i) for i in t[2]]
    sets = findTuple(t[3:])


    will result in:



    d1= [[7, -1, -2], [2, -2]]
    d2= [-6, 2, -5, 8]
    sets= [(3, 2), (6, 4), (2, 8), (3, 4), (0, 6), (6, 6), (7, 2)]





    share|improve this answer

























    • Thank you very much ,but I don't need to sum or subtract the d1 and d2, I need to keep them as them are.for example d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8]

      – LordNord
      Nov 15 '18 at 15:38











    • Updated the code, but this is a hack not a program.

      – Babak
      Nov 15 '18 at 22:00















    2














    Adding to what @DocDriven has done:



    with open('testt.txt',encoding='utf8')as f:
    text = f.read().strip()
    t = [i.split(" ") for i in text.splitlines() if len(i)>0 ]

    def findTuple(input):
    input = "".join(["".join(i) for i in input])
    pairs = input.replace("),","|").replace("(","").replace(")","").split("|")
    tuples=
    for pair in pairs:
    tuples.append(tuple(map(int,pair.split(","))))
    return tuples

    d1 = [list(map(int,i)) for i in t[:2]]
    d2 = [int(i) for i in t[2]]
    sets = findTuple(t[3:])


    will result in:



    d1= [[7, -1, -2], [2, -2]]
    d2= [-6, 2, -5, 8]
    sets= [(3, 2), (6, 4), (2, 8), (3, 4), (0, 6), (6, 6), (7, 2)]





    share|improve this answer

























    • Thank you very much ,but I don't need to sum or subtract the d1 and d2, I need to keep them as them are.for example d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8]

      – LordNord
      Nov 15 '18 at 15:38











    • Updated the code, but this is a hack not a program.

      – Babak
      Nov 15 '18 at 22:00













    2












    2








    2







    Adding to what @DocDriven has done:



    with open('testt.txt',encoding='utf8')as f:
    text = f.read().strip()
    t = [i.split(" ") for i in text.splitlines() if len(i)>0 ]

    def findTuple(input):
    input = "".join(["".join(i) for i in input])
    pairs = input.replace("),","|").replace("(","").replace(")","").split("|")
    tuples=
    for pair in pairs:
    tuples.append(tuple(map(int,pair.split(","))))
    return tuples

    d1 = [list(map(int,i)) for i in t[:2]]
    d2 = [int(i) for i in t[2]]
    sets = findTuple(t[3:])


    will result in:



    d1= [[7, -1, -2], [2, -2]]
    d2= [-6, 2, -5, 8]
    sets= [(3, 2), (6, 4), (2, 8), (3, 4), (0, 6), (6, 6), (7, 2)]





    share|improve this answer















    Adding to what @DocDriven has done:



    with open('testt.txt',encoding='utf8')as f:
    text = f.read().strip()
    t = [i.split(" ") for i in text.splitlines() if len(i)>0 ]

    def findTuple(input):
    input = "".join(["".join(i) for i in input])
    pairs = input.replace("),","|").replace("(","").replace(")","").split("|")
    tuples=
    for pair in pairs:
    tuples.append(tuple(map(int,pair.split(","))))
    return tuples

    d1 = [list(map(int,i)) for i in t[:2]]
    d2 = [int(i) for i in t[2]]
    sets = findTuple(t[3:])


    will result in:



    d1= [[7, -1, -2], [2, -2]]
    d2= [-6, 2, -5, 8]
    sets= [(3, 2), (6, 4), (2, 8), (3, 4), (0, 6), (6, 6), (7, 2)]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 22:02

























    answered Nov 15 '18 at 15:31









    BabakBabak

    486310




    486310












    • Thank you very much ,but I don't need to sum or subtract the d1 and d2, I need to keep them as them are.for example d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8]

      – LordNord
      Nov 15 '18 at 15:38











    • Updated the code, but this is a hack not a program.

      – Babak
      Nov 15 '18 at 22:00

















    • Thank you very much ,but I don't need to sum or subtract the d1 and d2, I need to keep them as them are.for example d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8]

      – LordNord
      Nov 15 '18 at 15:38











    • Updated the code, but this is a hack not a program.

      – Babak
      Nov 15 '18 at 22:00
















    Thank you very much ,but I don't need to sum or subtract the d1 and d2, I need to keep them as them are.for example d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8]

    – LordNord
    Nov 15 '18 at 15:38





    Thank you very much ,but I don't need to sum or subtract the d1 and d2, I need to keep them as them are.for example d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8]

    – LordNord
    Nov 15 '18 at 15:38













    Updated the code, but this is a hack not a program.

    – Babak
    Nov 15 '18 at 22:00





    Updated the code, but this is a hack not a program.

    – Babak
    Nov 15 '18 at 22:00













    1














    This is not the most concise solution, but since it is dynamic it provides flexibility and can be easily altered to handle new and/or additional data.



    # (Already exists)
    t = ['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']
    # ----------------
    dataCount = 3 # adjust this according to how many groups of data are expected (if known)

    values = ['' for _ in range(dataCount)]
    i = 0
    for e in lst:
    values[i] = values[i] + e # append the new data to the element
    if e == "" and values[i] != "": # advance to new element but ignore repeat "nulls"
    i = i + 1

    # various ways of displaying the data:
    print (values)
    for e in values:
    print (e)

    print (values[0], values[1], values[2])


    Output:



    ['7-1-22-2', '-62-58', '(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)']
    7-1-22-2
    -62-58
    (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)
    7-1-22-2 -62-58 (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)


    Instead of separate variables, I assigned each group of data to its own list element.






    share|improve this answer

























    • Thank you @Ctrl S , I will check soon, thanks for the notes

      – LordNord
      Nov 15 '18 at 14:45











    • If you need d1 and d2 to be integers, what happens to the -?

      – Ctrl S
      Nov 15 '18 at 14:47











    • It's a negative number,after I need to read each integer to do addition or subtraction ,So i think it will not work in this way, because I need to access each number and tuple

      – LordNord
      Nov 15 '18 at 15:08











    • The problem is to separate the whole number to digits, if all were positive it would be easier , you think is there any efficient way to convert it to d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8] even if they are strings, it's not important

      – LordNord
      Nov 15 '18 at 16:38











    • Why do you remove spaces in t = [i.replace(" ", "") for i in text.splitlines() ]? Without the spaces, you no longer know where one number ends and the next one begins. -6 2 turns into -62...

      – Ctrl S
      Nov 15 '18 at 17:36















    1














    This is not the most concise solution, but since it is dynamic it provides flexibility and can be easily altered to handle new and/or additional data.



    # (Already exists)
    t = ['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']
    # ----------------
    dataCount = 3 # adjust this according to how many groups of data are expected (if known)

    values = ['' for _ in range(dataCount)]
    i = 0
    for e in lst:
    values[i] = values[i] + e # append the new data to the element
    if e == "" and values[i] != "": # advance to new element but ignore repeat "nulls"
    i = i + 1

    # various ways of displaying the data:
    print (values)
    for e in values:
    print (e)

    print (values[0], values[1], values[2])


    Output:



    ['7-1-22-2', '-62-58', '(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)']
    7-1-22-2
    -62-58
    (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)
    7-1-22-2 -62-58 (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)


    Instead of separate variables, I assigned each group of data to its own list element.






    share|improve this answer

























    • Thank you @Ctrl S , I will check soon, thanks for the notes

      – LordNord
      Nov 15 '18 at 14:45











    • If you need d1 and d2 to be integers, what happens to the -?

      – Ctrl S
      Nov 15 '18 at 14:47











    • It's a negative number,after I need to read each integer to do addition or subtraction ,So i think it will not work in this way, because I need to access each number and tuple

      – LordNord
      Nov 15 '18 at 15:08











    • The problem is to separate the whole number to digits, if all were positive it would be easier , you think is there any efficient way to convert it to d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8] even if they are strings, it's not important

      – LordNord
      Nov 15 '18 at 16:38











    • Why do you remove spaces in t = [i.replace(" ", "") for i in text.splitlines() ]? Without the spaces, you no longer know where one number ends and the next one begins. -6 2 turns into -62...

      – Ctrl S
      Nov 15 '18 at 17:36













    1












    1








    1







    This is not the most concise solution, but since it is dynamic it provides flexibility and can be easily altered to handle new and/or additional data.



    # (Already exists)
    t = ['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']
    # ----------------
    dataCount = 3 # adjust this according to how many groups of data are expected (if known)

    values = ['' for _ in range(dataCount)]
    i = 0
    for e in lst:
    values[i] = values[i] + e # append the new data to the element
    if e == "" and values[i] != "": # advance to new element but ignore repeat "nulls"
    i = i + 1

    # various ways of displaying the data:
    print (values)
    for e in values:
    print (e)

    print (values[0], values[1], values[2])


    Output:



    ['7-1-22-2', '-62-58', '(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)']
    7-1-22-2
    -62-58
    (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)
    7-1-22-2 -62-58 (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)


    Instead of separate variables, I assigned each group of data to its own list element.






    share|improve this answer















    This is not the most concise solution, but since it is dynamic it provides flexibility and can be easily altered to handle new and/or additional data.



    # (Already exists)
    t = ['7-1-2', '2-2', '', '-62-58', '', '', '(3,2),(6,4),', '(2,8),(3,4),(0,6),', '(6,6),(7,2)']
    # ----------------
    dataCount = 3 # adjust this according to how many groups of data are expected (if known)

    values = ['' for _ in range(dataCount)]
    i = 0
    for e in lst:
    values[i] = values[i] + e # append the new data to the element
    if e == "" and values[i] != "": # advance to new element but ignore repeat "nulls"
    i = i + 1

    # various ways of displaying the data:
    print (values)
    for e in values:
    print (e)

    print (values[0], values[1], values[2])


    Output:



    ['7-1-22-2', '-62-58', '(3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)']
    7-1-22-2
    -62-58
    (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)
    7-1-22-2 -62-58 (3,2),(6,4),(2,8),(3,4),(0,6),(6,6),(7,2)


    Instead of separate variables, I assigned each group of data to its own list element.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 14:58

























    answered Nov 15 '18 at 14:14









    Ctrl SCtrl S

    645824




    645824












    • Thank you @Ctrl S , I will check soon, thanks for the notes

      – LordNord
      Nov 15 '18 at 14:45











    • If you need d1 and d2 to be integers, what happens to the -?

      – Ctrl S
      Nov 15 '18 at 14:47











    • It's a negative number,after I need to read each integer to do addition or subtraction ,So i think it will not work in this way, because I need to access each number and tuple

      – LordNord
      Nov 15 '18 at 15:08











    • The problem is to separate the whole number to digits, if all were positive it would be easier , you think is there any efficient way to convert it to d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8] even if they are strings, it's not important

      – LordNord
      Nov 15 '18 at 16:38











    • Why do you remove spaces in t = [i.replace(" ", "") for i in text.splitlines() ]? Without the spaces, you no longer know where one number ends and the next one begins. -6 2 turns into -62...

      – Ctrl S
      Nov 15 '18 at 17:36

















    • Thank you @Ctrl S , I will check soon, thanks for the notes

      – LordNord
      Nov 15 '18 at 14:45











    • If you need d1 and d2 to be integers, what happens to the -?

      – Ctrl S
      Nov 15 '18 at 14:47











    • It's a negative number,after I need to read each integer to do addition or subtraction ,So i think it will not work in this way, because I need to access each number and tuple

      – LordNord
      Nov 15 '18 at 15:08











    • The problem is to separate the whole number to digits, if all were positive it would be easier , you think is there any efficient way to convert it to d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8] even if they are strings, it's not important

      – LordNord
      Nov 15 '18 at 16:38











    • Why do you remove spaces in t = [i.replace(" ", "") for i in text.splitlines() ]? Without the spaces, you no longer know where one number ends and the next one begins. -6 2 turns into -62...

      – Ctrl S
      Nov 15 '18 at 17:36
















    Thank you @Ctrl S , I will check soon, thanks for the notes

    – LordNord
    Nov 15 '18 at 14:45





    Thank you @Ctrl S , I will check soon, thanks for the notes

    – LordNord
    Nov 15 '18 at 14:45













    If you need d1 and d2 to be integers, what happens to the -?

    – Ctrl S
    Nov 15 '18 at 14:47





    If you need d1 and d2 to be integers, what happens to the -?

    – Ctrl S
    Nov 15 '18 at 14:47













    It's a negative number,after I need to read each integer to do addition or subtraction ,So i think it will not work in this way, because I need to access each number and tuple

    – LordNord
    Nov 15 '18 at 15:08





    It's a negative number,after I need to read each integer to do addition or subtraction ,So i think it will not work in this way, because I need to access each number and tuple

    – LordNord
    Nov 15 '18 at 15:08













    The problem is to separate the whole number to digits, if all were positive it would be easier , you think is there any efficient way to convert it to d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8] even if they are strings, it's not important

    – LordNord
    Nov 15 '18 at 16:38





    The problem is to separate the whole number to digits, if all were positive it would be easier , you think is there any efficient way to convert it to d1 = [7,-1,-2,2,-2] d2 = [-6,2,-5,8] even if they are strings, it's not important

    – LordNord
    Nov 15 '18 at 16:38













    Why do you remove spaces in t = [i.replace(" ", "") for i in text.splitlines() ]? Without the spaces, you no longer know where one number ends and the next one begins. -6 2 turns into -62...

    – Ctrl S
    Nov 15 '18 at 17:36





    Why do you remove spaces in t = [i.replace(" ", "") for i in text.splitlines() ]? Without the spaces, you no longer know where one number ends and the next one begins. -6 2 turns into -62...

    – Ctrl S
    Nov 15 '18 at 17:36











    0














    Try this (notice that all variables are strings, not list of sets):



    d1 = "".join(t[:2])
    d2 = t[3]
    sets = "".join(t[6:])





    share|improve this answer























    • The problem comes to other inputs, the thing that helps divide d1,d2 and the sets are the spaces. they help you distinguish. Thank you for the try

      – LordNord
      Nov 15 '18 at 14:13















    0














    Try this (notice that all variables are strings, not list of sets):



    d1 = "".join(t[:2])
    d2 = t[3]
    sets = "".join(t[6:])





    share|improve this answer























    • The problem comes to other inputs, the thing that helps divide d1,d2 and the sets are the spaces. they help you distinguish. Thank you for the try

      – LordNord
      Nov 15 '18 at 14:13













    0












    0








    0







    Try this (notice that all variables are strings, not list of sets):



    d1 = "".join(t[:2])
    d2 = t[3]
    sets = "".join(t[6:])





    share|improve this answer













    Try this (notice that all variables are strings, not list of sets):



    d1 = "".join(t[:2])
    d2 = t[3]
    sets = "".join(t[6:])






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 14:04









    DocDrivenDocDriven

    1,2652721




    1,2652721












    • The problem comes to other inputs, the thing that helps divide d1,d2 and the sets are the spaces. they help you distinguish. Thank you for the try

      – LordNord
      Nov 15 '18 at 14:13

















    • The problem comes to other inputs, the thing that helps divide d1,d2 and the sets are the spaces. they help you distinguish. Thank you for the try

      – LordNord
      Nov 15 '18 at 14:13
















    The problem comes to other inputs, the thing that helps divide d1,d2 and the sets are the spaces. they help you distinguish. Thank you for the try

    – LordNord
    Nov 15 '18 at 14:13





    The problem comes to other inputs, the thing that helps divide d1,d2 and the sets are the spaces. they help you distinguish. Thank you for the try

    – LordNord
    Nov 15 '18 at 14:13











    0














    Thank you all for helping me, big thanks to @Ctrl S ,I solved the problem.



    with open(txt,encoding='utf8')as f:
    text = f.read().strip()
    lists = ['','','']
    listindex = 0
    for i in text.splitlines():
    if i:
    lists[listindex] += i
    if i == "" and lists[listindex] != "":
    listindex += 1
    d1,d2,sets = lists


    to convert the sets to tuples , I just used the eval().






    share|improve this answer



























      0














      Thank you all for helping me, big thanks to @Ctrl S ,I solved the problem.



      with open(txt,encoding='utf8')as f:
      text = f.read().strip()
      lists = ['','','']
      listindex = 0
      for i in text.splitlines():
      if i:
      lists[listindex] += i
      if i == "" and lists[listindex] != "":
      listindex += 1
      d1,d2,sets = lists


      to convert the sets to tuples , I just used the eval().






      share|improve this answer

























        0












        0








        0







        Thank you all for helping me, big thanks to @Ctrl S ,I solved the problem.



        with open(txt,encoding='utf8')as f:
        text = f.read().strip()
        lists = ['','','']
        listindex = 0
        for i in text.splitlines():
        if i:
        lists[listindex] += i
        if i == "" and lists[listindex] != "":
        listindex += 1
        d1,d2,sets = lists


        to convert the sets to tuples , I just used the eval().






        share|improve this answer













        Thank you all for helping me, big thanks to @Ctrl S ,I solved the problem.



        with open(txt,encoding='utf8')as f:
        text = f.read().strip()
        lists = ['','','']
        listindex = 0
        for i in text.splitlines():
        if i:
        lists[listindex] += i
        if i == "" and lists[listindex] != "":
        listindex += 1
        d1,d2,sets = lists


        to convert the sets to tuples , I just used the eval().







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 8:08









        LordNordLordNord

        707




        707



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320307%2fassign-to-variables-from-text-file%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo