LordNord
LordNord

Reputation: 123

Assign to variables from text file

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

Upvotes: 1

Views: 88

Answers (4)

LordNord
LordNord

Reputation: 123

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

Upvotes: 0

Babak
Babak

Reputation: 613

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

Upvotes: 2

Ctrl S
Ctrl S

Reputation: 1103

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.

Upvotes: 1

DocDriven
DocDriven

Reputation: 3974

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

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

Upvotes: 0

Related Questions