Python Forum
Go to line starting with and variable not detected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Go to line starting with and variable not detected
#1
Hi, how can I go a line starting with a precise word ? After my first condition, I need to go to the line starting with "/22/" (I have to browse my file in different directions) and add new_name to the new_line, but the result of my first condition isn't persisted through the code...

for fichier in os.listdir("result/"):
    if not fnmatch.fnmatch(fichier, 'file_0.txt'):
        with fileinput.input(["result/"+fichier], inplace=1) as file_X :
            # file_X.write('\r\ncoucou')
            print("---------------------------------")
            for line in file_X:
                line = line.strip('\n')
                if line.startswith("/54/"):
                    split = line.split("/")
                    num = split[2]
                    # print(num)
                    for pj in pjCSV:
                        if num in pj:
                            ...
                            new_name = "jacques"
                            sys.stderr.write(new_name + "\r\n")
                if line.startswith('/22/'):
                    new_line = line + new_name # new_name is not detected
                    print(line.replace(line, new_line))
                    continue
                print (line.rstrip('\n')) # ecriture de toutes les lignes
 
            break
Reply
#2
I don't understand. You're saying the result of the first condition is not persisted. But isn't the result new_name? That appears to be persisted. Or are there cases where you don't find a new name, so you want to skip the bit with '/22/'?

Why do you have to go back and forth through the file? That's not easy in Python, so you'd be better off putting more persistence into your code to remember information it might need later, if you can.

Edit: If you really need to go back and forth, and the file is not too large, pull all the lines at once into a list. Searching back and forth through the list is much easier.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
new_name is supposed to be used in the second condition (new_name can not be empty or null), but when I try to use new_name in the second condition, i have this

NameError: name 'new_name' is not defined
Reply
#4
In your code, it seems that new_name is only defined if some conditions have occurred: a line starting with /54/ is needed before, and num in pj needs to be satisfied, perhaps other conditions that we don't see. The error probably means that some of these necessary conditions failed. You could perhaps write
new_name = 'DEFAULT_NAME'
at the top of the file to see if this name is used.
Reply
#5
the name used is
new_name = 'DEFAULT_NAME'
but i need the new_name to be the new_name from the if. I believe that because the "/54/" line is place after the "/22/" line, this could cause the issue, how can i fix it ? (When I read the /54/ line, i need to go back to find the /22/ line)
Reply
#6
I found a temporary solution, I've made several with fileinput.input, so that my first is looping on /54/ and the second is looping on /22/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python not detected on computer adriennn 3 930 Nov-18-2023, 02:46 PM
Last Post: snippsat
  My .exe made using Python being detected as a virus 100grassfed 2 3,003 Jun-16-2021, 04:41 AM
Last Post: buran
  Finding line numbers starting with certain string Sutsro 3 2,535 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  python --version yields no output, not detected by other programs ten 3 3,419 Jun-25-2020, 04:48 AM
Last Post: perfringo
  "Widget Javascript not detected" error BobLoblaw 0 4,297 Oct-07-2017, 04:48 PM
Last Post: BobLoblaw
  Object Detection that records the number of secs the face is not detected trabis03 1 2,542 Jul-21-2017, 04:14 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020