2

So I have this text file

(1, 15), 'indice3 = [4, 5, 6]'
(7, 1), "indice1 = {3: 'A B C'}"
(11, 7), "indice4 = '(1, 2)'"
(11, 17), 'typage = mode de déclaration des types de variable'
(23, 5), "indice5 = '(3, 4)'"
(25, 1), '27 * 37 = 999'

As you can see, there's at first coordinates and then a text.

Here's an example of what it would look like at the end (for the first two elements)

{
    (1,15): "indice3 = [4, 5, 6]",
    (7, 1): "indice1 = {3: 'A B C'}"
}

I know that I should start by reading the file (f = open("dico_objets.txt", "r")) and then read it line by line but I don't know how to split it correctly.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • Does this answer your question? [How to convert string representation of list to a list](/q/1894269/4518341). (The fact that it's about a list is beside the point. And in your case, each line seems to represent a tuple.) – wjandrea Nov 15 '22 at 21:03

2 Answers2

2

Try to use ast.literal_eval:

from ast import literal_eval


out = []
with open("your_file.txt", "r") as f_in:
    for line in map(str.strip, f_in):
        if line == "":
            continue
        out.append(literal_eval(f"[{line}]"))

print(dict(out))

Prints:

{
    (1, 15): "indice3 = [4, 5, 6]",
    (7, 1): "indice1 = {3: 'A B C'}",
    (11, 7): "indice4 = '(1, 2)'",
    (11, 17): "typage = mode de déclaration des types de variable",
    (23, 5): "indice5 = '(3, 4)'",
    (25, 1): "27 * 37 = 999",
}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Why bother with the f-string and list of lists? `out = {}; ...; k, v = literal_eval(line); out[k] = v; ...; print(out)` – wjandrea Nov 15 '22 at 21:09
  • Why bother with `str.strip`? OP's data doesn't have any lines that'd need that. If it's for the `if line == "":`, you could just do it there instead: `if line.strip() == "":`. – wjandrea Nov 15 '22 at 21:10
  • @wjandrea The `str.strip` prevents followup questions, that the script throws an exception and/or there are redundant data in the output. Its probable that the file contains empty lines, empty string at the beginning etc. – Andrej Kesely Nov 15 '22 at 21:17
1

you can use find() on each line. Read your file line by line and do something like below

split_point = line.find(',', line.find(',')+1)
out_dict[line[:split_point]] = line[split_point:]
vamsikakuru
  • 11
  • 1
  • 2
  • @AndrejKesely Try it and look at the output. It's nothing like OP wants. The keys are strings instead of tuples of ints, and the values have a bunch of extra junk. It's a good first step, but you'd need to add a bunch more parsing to get it like OP wants. – wjandrea Nov 15 '22 at 21:18
  • @wjandrea Yeah, the keys should be evaled to tuple..., strip `, ` from the values. – Andrej Kesely Nov 15 '22 at 21:21
  • @wjandrea I agree with you that OP stated an example of output but he mainly asked on how to split the line to which I think my answer is simple and clear – vamsikakuru Nov 15 '22 at 21:23
  • @vamsikakuru Alright, yeah, fair enough. I was paying more attention to the question in the title, but OP did also say "I don't know how to split it correctly". – wjandrea Nov 15 '22 at 21:25
  • Personally, I'd rather use `.split()`, something like this: `x, y, v = line.split(',', 2); out_dict[x, y] = v` – wjandrea Nov 15 '22 at 21:32