problem for write and delete a line in text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeepers
    New Member
    • Mar 2007
    • 3

    problem for write and delete a line in text file

    Hi all,
    my problem is delete a line in a text file.

    the file contain -n line with user, userCode.
    i want to replace the line with user is egual at user, and/or insert new line but delete the line like user.
    i'm sorry for my english :D

    if user exist delete a line of the user and insert new line.
    if not exist insert new line.
    possibility to delete a line.

    please :D

    Code:
    user="myname"
    userCode="1987987981"
    f = open('userCodes.txt', 'r+')
    for line in f:
       
        if user in line:
            print 'prima ' + str(line)
            line = line.replace(line, user + ',' + str(userCode) + '\n')
            f.write('\n' +str(line))
            print 'dopo ' + str (line)
  • jeepers
    New Member
    • Mar 2007
    • 3

    #2
    This code is ok for my work.

    Code:
    user="myname"
    userCode="1987987981"
    import re
    import fileinput, string, sys
    ##f = open('userCodes.txt', 'rw')
     ##print f
    ##f = open('userCodes.txt', 'r+')
    f= file('userCodes.txt')
    ##print f
    	###scorro le linee del file
    newlines=[]
    for line in f:
                if user in line:  
                    print 'prima raplace ' + str(line)
                    line = line.replace(line, user + ',' + str(userCode) + '\n')
                    print 'dopo replace ' + str(line)
                newlines.append(line)
    outfile=file('userCodes.txt','w')
    outfile.writelines(newlines)

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by jeepers
      This code is ok for my work.

      Code:
      user="myname"
      userCode="1987987981"
      import re
      import fileinput, string, sys
      ##f = open('userCodes.txt', 'rw')
       ##print f
      ##f = open('userCodes.txt', 'r+')
      f= file('userCodes.txt')
      ##print f
      	###scorro le linee del file
      newlines=[]
      for line in f:
                  if user in line:  
                      print 'prima raplace ' + str(line)
                      line = line.replace(line, user + ',' + str(userCode) + '\n')
                      print 'dopo replace ' + str(line)
                  newlines.append(line)
      outfile=file('userCodes.txt','w')
      outfile.writelines(newlines)
      a little refinement
      Code:
      user="myname"
      userCode="1987987981"
      for line in open('userCodes.txt'):
                  line = line.strip() 
                  if user in line:                  
                      line = line.replace(line, user + ',' + str(userCode) + '\n')
                      print line
      you are only generating one output file right? you can save some code by just printing to stdout. then when you execute the script on your OS ,
      Code:
      # python yourscript.py > output.txt

      Comment

      • jeepers
        New Member
        • Mar 2007
        • 3

        #4
        Thank's ghostdog74,
        i have multiple code to write in file.txt when the user change or delete your accont.
        In the test user and userCodes, is defined for semplify my work, but in the real script user and userCodes appartein at the user logged in.
        If the user not exist in database also new line in the file .

        Again sorry for my english.

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          Originally posted by jeepers
          Thank's ghostdog74,
          i have multiple code to write in file.txt when the user change or delete your accont.
          In the test user and userCodes, is defined for semplify my work, but in the real script user and userCodes appartein at the user logged in.
          If the user not exist in database also new line in the file .

          Again sorry for my english.
          sorry don't understand, but anyway, to output to different files in your script, use the method of opening files with "a" or "w" and write the data to it
          eg
          Code:
          f = open("file","a")
          f.write("a string\n")
          f.close()
          or just simply
          Code:
          open("file","a').write("a string\n")

          Comment

          Working...