omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    3
    18
    2672
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DavinE
      DavinE last edited by

      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.

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @DavinE last edited by cvp

        @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]}')
        
        1 Reply Last reply Reply Quote 1
        • DavinE
          DavinE last edited by

          @cvp, du bist der beste.... weist du wie lange ich daran gestern gesessen haben 😭...

          Vielen dank 🙏

          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @DavinE last edited by

            @DavinE said

            du bist der beste.... weist du wie lange ich daran gestern gesessen haben

            Thanks to Google translate

            DavinE 1 Reply Last reply Reply Quote 0
            • DavinE
              DavinE @cvp last edited by

              @cvp
              Sorry wrong Language 😅

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @DavinE last edited by

                @DavinE No problem, it is a way to learn a new one 😉

                DavinE 1 Reply Last reply Reply Quote 0
                • DavinE
                  DavinE @cvp last edited by

                  @cvp

                  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;29413

                  1052466 — 29413
                  .....
                  ....
                  ...
                  ..
                  1596491 — 4817
                  Count: 7 Numbers.

                  Is there a Way to read the Numbers ?

                  cvp 2 Replies Last reply Reply Quote 0
                  • cvp
                    cvp @DavinE last edited by

                    @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]}")
                    
                    1 Reply Last reply Reply Quote 1
                    • JonB
                      JonB last edited by JonB

                      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 or filter or list comprehension to act on the entire list efficiently.

                      DavinE 1 Reply Last reply Reply Quote 1
                      • cvp
                        cvp @DavinE last edited by cvp

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

                        DavinE 2 Replies Last reply Reply Quote 0
                        • DavinE
                          DavinE @cvp last edited by

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

                          1 Reply Last reply Reply Quote 0
                          • DavinE
                            DavinE @cvp last edited by

                            @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

                            cvp 1 Reply Last reply Reply Quote 0
                            • cvp
                              cvp @DavinE last edited by

                              @DavinE 👍

                              1 Reply Last reply Reply Quote 1
                              • DavinE
                                DavinE @JonB last edited by

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

                                cvp 1 Reply Last reply Reply Quote 0
                                • cvp
                                  cvp @DavinE last edited by

                                  @DavinE quicker for loop, only on '2' elements:

                                  		for i in range(3,len(row),9):
                                  
                                  DavinE 1 Reply Last reply Reply Quote 0
                                  • DavinE
                                    DavinE @cvp last edited by

                                    @cvp

                                    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 :(

                                    1 Reply Last reply Reply Quote 0
                                    • JonB
                                      JonB last edited by

                                      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?

                                      DavinE 1 Reply Last reply Reply Quote 0
                                      • DavinE
                                        DavinE @JonB last edited by

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

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post
                                        Powered by NodeBB Forums | Contributors