Need help by read a file
-
Hello guys,
I Have an new Problem witch i can‘t solve....
This is my File Name:
rDatpreis -- 2022_03_01 -- short
This is my content: (exemple more then 50.000 Lines)
P;A;1001009;2;602;;;;;;;1001010;2;602;;;;;;;1001011;2;602;;;;;;; P;A;1001012;2;602;;;;;;;1001013;2;602;;;;;;;1001014;2;602;;;;;;; P;A;1001016;2;602;;;;;;;1001017;2;568;;;;;;;1001018;2;568;;;;;;; P;A;1001019;2;568;;;;;;;1001020;2;568;;;;;;;1001021;2;568;;;;;;; P;A;1001022;2;620;;;;;;;1001023;2;568;;;;;;;1001024;2;564;;;;;;; P;A;1001025;2;1083;;;;;;;1001026;2;1083;;;;;;;1001027;2;1083;;;;;;; P;A;1001028;2;1083;;;;;;;1001029;2;1083;;;;;;;1001030;2;1083;;;;;;; P;A;1001031;2;1083;;;;;;;1001033;2;1600;;;;;;;1001034;2;1596;;;;;;; P;A;1001035;2;1600;;;;;;;1001037;2;2293;;;;;;;1001038;2;2289;;;;;;; P;A;1001039;2;2293;;;;;;;1001041;2;4382;;;;;;;1001042;2;4382;;;;;;; P;A;1001043;2;4382;;;;;;;1001047;2;6688;;;;;;;1001048;2;6634;;;;;;; P;A;1001051;2;12838;;;;;;;1001054;2;19309;;;;;;;1001055;2;19309;;;;;;; P;A;1001057;2;26985;;;;;;;1001058;2;33501;;;;;;;1001059;2;33501;;;;;;; P;A;1001060;2;45665;;;;;;;1001061;2;44410;;;;;;;1001076;2;449;;;;;;; P;A;1001077;2;522;;;;;;;1001078;2;449;;;;;;;1001079;2;449;;;;;;; P;A;1001080;2;449;;;;;;;1001081;2;449;;;;;;;1001082;2;449;;;;;;; P;A;1001083;2;449;;;;;;;1001084;2;449;;;;;;;1001085;2;447;;;;;;; P;A;1001086;2;521;;;;;;;1001087;2;522;;;;;;;1001088;2;522;;;;;;; P;A;1001089;2;522;;;;;;;1001090;2;521;;;;;;;1001091;2;522;;;;;;; P;A;1001092;2;522;;;;;;;1001093;2;522;;;;;;;1001094;2;667;;;;;;;
I need a way thus i can read this content like this:
for row in content_file: print(f'row_0 -- {row[0]}') — P print(f'row_1 -- {row[1]}') — A print(f'row_2 -- {row[2]}') — 1001009 print(f'row_3 -- {row[3]}') — 2 print(f'row_4 -- {row[4]}') — 602 ..... .... ... .. . For each row in the file
But i didn‘t get it how to solve it.....
(It‘s not a .csv file)
I Hope someone can help me out.
-
Note that the
csv
module should work, there is a way to specify a different delimiter';'
in this case. That might make things easier?import csv with open(filename, 'r') as f: reader = csv.reader(f, delimiter=';') for row in reader: #...
Or, you could use csv.DictReader in a similar way where you define field names -- then you don't have to remember that field 26 is the number you want, you'd give it a useful name. (If you read the whole file into a list of Dicts, you could do things like use
map
orfilter
or list comprehension to act on the entire list efficiently.
-
@DavinE I saw that you did connect to this forum some hours ago. I know that @JonB gave a more smart and technical way to solve your problem but did you test the last small script I posted?
-
@cvp said:
@DavinE I saw that you did connect to this forum some hours ago. I know that @JonB gave a more smart and technical way to solve your problem but did you test the last small script I posted?
No, i only read it.
Now i'm testing it, i reply to you.
-
@cvp said:
@DavinE I saw that you did connect to this forum some hours ago. I know that @JonB gave a more smart and technical way to solve your problem but did you test the last small script I posted?
Yes, it works for me in both Situations. :D
-
@DavinE 👍
-
@JonB said:
Note that the
csv
module should work, there is a way to specify a different delimiter';'
in this case. That might make things easier?i tried it too but in my case it don't worked....
your example works .... strange@JonB said:
import csv with open(filename, 'r') as f: reader = csv.reader(f, delimiter=';') for row in reader: #...
Or, you could use csv.DictReader in a similar way where you define field names -- then you don't have to remember that field 26 is the number you want, you'd give it a useful name. (If you read the whole file into a list of Dicts, you could do things like use
map
orfilter
or list comprehension to act on the entire list efficiently.Okay, but the above example is suitable for me since I always get the file without column name I do that with the numbers.
-
@DavinE quicker for loop, only on '2' elements:
for i in range(3,len(row),9):
-
Thanks, but I saw yesterday that what I wanted did not work....
My wholesalers do not output all the data.... there are missing materials... now I want to try Selenium which I now have to run externally :(
-
Can you explain better what isn't working?
Is the issue that the number of fields changes? Or that sometimes some fields are not populated?
-
@JonB said:
Can you explain better what isn't working?
Is the issue that the number of fields changes? Or that sometimes some fields are not populated?
This has nothing to do with your code....
My wholesalers do not provide complete material lists...
I now retrieve my prices via my Raspberry PI (Selenium) takes a little longer but there I can get all the prices.