Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Write at the beginn of a File (.py)
Forum Updated to NodeBB v4.3 + New Features

Write at the beginn of a File (.py)

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 6 Posters 3.0k Views 1 Watching
  • 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.
  • N NotYourFan

    Oh now i have another problem ...

    at the first four lines are my imports for the script:

    import csv
    import re
    import json
    import math

    i want that to write now "test = 'TestFile.csv'"
    at the fifth line, to looks like this:

    import csv
    import re
    import json
    import math
    
    test = 'TestFile.csv'
    
    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by J.Hilk
    #10

    @NotYourFan

    QFile file("C:/Users/Test/Desktop/Projekt/write.py");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
      QTextStream rstream(&file);
      QVector<QString> v;
       while(! rstream.atEnd())
            v.append(rstream.readLine());
      file.close();
      if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream wstream(&file);
    
        for(int i(0); i < v.size(); i++){
             if( i == 5)
                 wstream <<  "test = 'TestFile.csv' " 
            wstream << v.at(i);
    
        }
        file.close();
      }
    }
    

    edit: fixed some typos, auto correct can be a pain, sometimes.


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    JonBJ 1 Reply Last reply
    3
    • J.HilkJ J.Hilk

      @NotYourFan

      QFile file("C:/Users/Test/Desktop/Projekt/write.py");
      if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream rstream(&file);
        QVector<QString> v;
         while(! rstream.atEnd())
              v.append(rstream.readLine());
        file.close();
        if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
          QTextStream wstream(&file);
      
          for(int i(0); i < v.size(); i++){
               if( i == 5)
                   wstream <<  "test = 'TestFile.csv' " 
              wstream << v.at(i);
      
          }
          file.close();
        }
      }
      

      edit: fixed some typos, auto correct can be a pain, sometimes.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #11

      @J.Hilk
      I don't wish to start an argument or sound rude, but why encourage to read the whole file into memory when it's just as simple to do it line by line (at least in OP's case) as you go? Plus read-from-old-write-to-new doesn't suffer from blatting the original when the programmer makes a coding error! :) Anyway it's preference for the OP.

      J.HilkJ 1 Reply Last reply
      1
      • JonBJ JonB

        @J.Hilk
        I don't wish to start an argument or sound rude, but why encourage to read the whole file into memory when it's just as simple to do it line by line (at least in OP's case) as you go? Plus read-from-old-write-to-new doesn't suffer from blatting the original when the programmer makes a coding error! :) Anyway it's preference for the OP.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #12

        @JonB Simple,
        I took the liberty to copy and past @VRonin example from before and edit it ;-)


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        JonBJ 1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @JonB Simple,
          I took the liberty to copy and past @VRonin example from before and edit it ;-)

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #13

          @J.Hilk
          Lol! :) OK, I commented on that earlier, far be it for us to disagree with the venerable @VRonin :) Anyway, the OP can do it either way as he sees fit.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NotYourFan
            wrote on last edited by
            #14

            @J-Hilk

            Wehen i run your code, my .py scriptcode changed complete to only ONE Row.

            J.HilkJ 1 Reply Last reply
            0
            • N NotYourFan

              @J-Hilk

              Wehen i run your code, my .py scriptcode changed complete to only ONE Row.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #15

              @NotYourFan
              the example is not checked, I don't have a +py file for that.

              Obviously you need to add a new line character at the end of each line.

              if( i == 5)
                           wstream <<  "test = 'TestFile.csv' "  << "\n";
                      wstream << v.at(i) << "\n";;
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • N Offline
                N Offline
                NotYourFan
                wrote on last edited by
                #16
                This post is deleted!
                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NotYourFan
                  wrote on last edited by
                  #17

                  @J-Hilk
                  now it works the problem is when i changed the wstram it doesent override.

                  i get " test = 'Hallo221.csv' test = 'Hallo1.csv' ...."

                  how i can override?

                  thx

                  JonBJ 1 Reply Last reply
                  0
                  • N NotYourFan

                    @J-Hilk
                    now it works the problem is when i changed the wstram it doesent override.

                    i get " test = 'Hallo221.csv' test = 'Hallo1.csv' ...."

                    how i can override?

                    thx

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #18

                    @NotYourFan
                    What does "override" mean? Do you mean "overwrite" what is already in wstream? Do you mean wstream.seek(0)?

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      NotYourFan
                      wrote on last edited by
                      #19

                      yeah sry,

                      i mean "overwrite".

                      i always get " test = 'Hallo221.csv' test = 'Hallo1.csv' test = 'Hallo12321.csv' .....

                      but i want to get only "test = 'Hallo12321.csv' so i want to "overwrite" the line 5 and not add a new string

                      JonBJ J.HilkJ 2 Replies Last reply
                      0
                      • N NotYourFan

                        yeah sry,

                        i mean "overwrite".

                        i always get " test = 'Hallo221.csv' test = 'Hallo1.csv' test = 'Hallo12321.csv' .....

                        but i want to get only "test = 'Hallo12321.csv' so i want to "overwrite" the line 5 and not add a new string

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #20

                        @NotYourFan
                        So did you try my wstream.seek(0)? after the first write and before the second?
                        Or, depending on just what you intend by "overwrite", you may need to wstream.resize(0).
                        Try with both "first-string-is-long", "second-is-short" and then with "first-is-short", "second-string-is-long" to see the difference between seek(0) vs resize(0).

                        1 Reply Last reply
                        0
                        • N NotYourFan

                          yeah sry,

                          i mean "overwrite".

                          i always get " test = 'Hallo221.csv' test = 'Hallo1.csv' test = 'Hallo12321.csv' .....

                          but i want to get only "test = 'Hallo12321.csv' so i want to "overwrite" the line 5 and not add a new string

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by J.Hilk
                          #21

                          @NotYourFan

                          You're doing fishy stuff, that may break at any point. 
                          You should rethink what you're actually trying to do.

                          for(int i(0); i < v.size(); i++){
                                   if( i == 5) {
                                       wstream <<  "test = 'TestFile.csv' "   << "\n";
                                      if(v.at(i).contains("test = '"))
                                          continue;
                                   }        
                                   wstream << v.at(i)  << "\n";
                              }
                          

                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          N 1 Reply Last reply
                          3
                          • J.HilkJ J.Hilk

                            @NotYourFan

                            You're doing fishy stuff, that may break at any point. 
                            You should rethink what you're actually trying to do.

                            for(int i(0); i < v.size(); i++){
                                     if( i == 5) {
                                         wstream <<  "test = 'TestFile.csv' "   << "\n";
                                        if(v.at(i).contains("test = '"))
                                            continue;
                                     }        
                                     wstream << v.at(i)  << "\n";
                                }
                            
                            N Offline
                            N Offline
                            NotYourFan
                            wrote on last edited by
                            #22

                            @J.Hilk
                            PERFEKT !!!!!
                            THANK YOU SO MUTCH

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved