I have a sample.txt file:
1 7 14
3 3 10
1 1 3
What I wrote (it is wrong but at least something)(there were better ones, but I couldn't remake them):
content = []
with open ("sample.txt","r",encoding="utf-8") as file:
for line in file:
line = file.readline().strip().split(" ")
mydict = {"A":int(file.readline().strip().split(" ")[0]),
"B":int(file.readline().strip().split(" ")[1]),
"C":int(file.readline().strip().split(" ")[2])}
content.append(mydict)
print (content)
expected output:
[ {"A":1,"B":7,"C":14},{"A":3,"B":3,"C":10},{"A":1,"B":1,"C":3} ]
Neither of them worked sadly. There were 3 problems typically:
- When I try to convert the values with int()
- First or last line is missing (problem with indexing)
- At the values I can't write just simply line[0], because that way it is not working (why?)
I am really newbie, maybe it will be good for someone else who is doing the same as me and noob like me.