Search code examples
pythonreadlinenon-ascii-characters

Python file reding and splitting


file ="data.txt"
data=[]
with open(file) as i:
    data=i.readlines()    
for i in data:
        a=i.replace(".",";")
        a.split(";")
        newdata.append(a)
        print(a[1])

So if I know well data now is a list of strings, is it? I want to replace the . with ; and split it into a list of strings and save it.

example data.txt:

  1. grillsütő; jó állapotú; 5000
  2. gyerek bicikli; 14"-os; 10000

If I know well after the reading the data variable:

[1. grillsütő; jó állapotú; 5000] //0.index

[4. gyerek bicikli; 14"-os; 10000] //1.index

and what I want:

[[1][ grillsütő][ jó állapotú][ 5000]] //0.index

[[4][ gyerek bicikli][ 14"-os][ 10000]] //[1][1]:[ gyerek bicikli]

What am I missing?


Solution

  • example snippet

    str = "1;2;3;4"
    parts = str.split(";")
    final_array = [[e] for e in parts]
    print final_array
    

    output [['1'], ['2'], ['3'], ['4']]