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
-
@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.