Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
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. -
@DavinE you could open the file as text file, read it line by line then split each line in an array
with open('a7.txt') as f: for line in f: row = line.split(';') print(f'row_0 -- {row[0]}') print(f'row_1 -- {row[1]}') print(f'row_2 -- {row[2]}') print(f'row_3 -- {row[3]}') print(f'row_4 -- {row[4]}')
-
@cvp, du bist der beste.... weist du wie lange ich daran gestern gesessen haben 😭...
Vielen dank 🙏
-
@DavinE said
du bist der beste.... weist du wie lange ich daran gestern gesessen haben
Thanks to Google translate
-
@cvp
Sorry wrong Language 😅 -
@DavinE No problem, it is a way to learn a new one 😉
-
I have an other Question about my Problem above, i‘ve Seen that in one row are more then one number i need.....
In the First case ist Looks like this:
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;;;;;;;
I need each two Numbers:
1001009
—602
.....
....
...
..
1001024
—564
Count: 15 Numbers.In my Second case:
P;A;1052466;1;48238;;;;;;;1052466;2;29413;;;;;;;1161801;1;2440;;;;;;; P;A;1161801;2;2420;;;;;;;1161825;1;6990;;;;;;;1161825;2;6990;;;;;;; P;A;1161903;1;3190;;;;;;;1161903;2;3190;;;;;;;1388475;1;203500;;;;;;; P;A;1388475;2;175955;;;;;;;1590111;1;1630;;;;;;;1590111;2;994;;;;;;; P;A;1596491;1;5700;;;;;;;1596491;2;4817;;;;;;;1596492;1;5700;;;;;;;
Here i need only the two Numbers with the 2:
1052466;2
;294131052466
—29413
.....
....
...
..
1596491
—4817
Count: 7 Numbers.Is there a Way to read the Numbers ?
-
@DavinE try
with open('a7.txt') as f: for line in f: row = line.split(';') for i in range(len(row)): if row[i] == '2': print(f"{row[i-1]}-{row[i+1]}")
-
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 👍
-
@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.