1

Im kinda new to python, im trying to the basic task of splitting string data from a file using a double backslash (\\) delimiter. Its failing, so far:

    from tkinter import filedialog
    import string
    import os

    #remove previous finalhostlist
    try:
        os.remove("finalhostlist.txt")
    except Exception as e: 
      print (e)

    root = tk.Tk()
    root.withdraw()
    print ("choose hostname target list")
    file_path = filedialog.askopenfilename()

    with open("finalhostlist.txt", "wt") as rawhostlist:
        with open(file_path, "rt") as finhostlist:
            for line in finhostlist:
##            rawhostlist.write("\n".join(line.split("\\\\")))
              rawhostlist.write(line.replace(r'\\', '\n'))

I need the result to be from e.g.
\\Accounts01\\Accounts02
to
Accounts01 Accounts02

Can someone help me with this? I'm using python 3.

EDIT: All good now, strip("\\") on its own did it for me.

Thanks guys!

3
  • So what do you expect to be written? You have a list of strings, your split succeeded. Commented Mar 23, 2015 at 20:33
  • Why are you importing tkinter two different ways? Also, you have an indentation error. Commented Mar 25, 2015 at 19:47
  • oh that was before I fixed it. thanks for pointing it out. Edited. Commented Mar 25, 2015 at 23:44

3 Answers 3

1

write expects a string and you have passed it a list, if you want the contents written use str.join.

rawhostlist.write("\n".join(line.split("\\")))

You also don't need to call close when you use with, it closes your file automatically and you actually never call close anyway as you are missing parens rawhostlist.close -> rawhostlist.close()

It is not clear if you actually have 2,3 or 4 backslashes. Your original code has two, your edit has three so whichever it is you need to use to same amount to split.

In [66]: s = "\\Accounts01\\Accounts02"
In [67]: "\n".join(s.split("\\\\"))
Out[67]: '\\Accounts01\\Accounts02'    
In [68]: s = "\\\Accounts01\\\counts02"    
In [69]: "\n".join(s.split("\\\\"))
Out[69]: '\nAccounts01\ncounts02'

If it varies then split with \\ and filter empty strings.

Looking at the file you posted, you have a single element on each line so simply use strip

with open("finalhostlist.txt", "wt") as f_out, open(infile, "rt") as f_in:
        for line in f_in:
            out.write(line.strip("\\"))

Output:

ACCOUNTS01    
EXAMS01
EXAMS02                                                        
RECEPTION01
RECEPTION02
RECEPTION03
RECEPTION04
RECEPTION05
TEACHER01  
TEACHER02                                                            
TEACHER03
TESTCENTRE-01        
TESTCENTRE-02
TESTCENTRE-03  
TESTCENTRE-04  
TESTCENTRE-05  
TESTCENTRE-06  
TESTCENTRE-07  
TESTCENTRE-08  
TESTCENTRE-09  
TESTCENTRE-10  
TESTCENTRE-11  
TESTCENTRE-12  
TESTCENTRE-13  
TESTCENTRE-14  
TESTCENTRE-15
Sign up to request clarification or add additional context in comments.

13 Comments

This just writes out the elements concatenated. file.writelines() doesn't add newlines in between. That may be fine, but it is far from clear what the expected output is, really.
Thanks for the answer, it is giving me a strange output though :( \\ACCOUNTS01 ,,"ACCOUNTS01 " and lots of " and .end of file
@PadraicCunningham hey, its better! but still... im getting an output with duplicate entries, like so here
ah thanks, it works now. However, it is also making double empty lines between entries, would there be a way to remove them?
@pzp1997, because that is what the OP's code was doing long before you answered, the questions has been edited a few times. Your code will also fail unless there are exactly the same amount of "\\" so splitting and filtering may be the only solution but without being a mind reader or seeing all the data it is kind of hard to tell. Also splitting a couple of words or using replace is not exactly going to matter much in this example, we are talking nanoseconds. To be honest I am not sure the OP fully knows what is happening in the code.
|
1

I would do rawhostlist.write(line.replace(r'\\', '\n')). If you want a little more efficiency feel free to use re.sub() instead, but I don't think it will make much of a difference here. There's no need to call .write() for each line. And there is definitely no need to convert the string into a list -- just to convert it back into a string!

Comments

0

if you want them written on separate lines:

   for sub in line.split("\\"):rawhostlist.write(sub)

1 Comment

This one gets rid of `\` but duplicates on the same line, \\ACCOUNTS01ACCOUNTS01 \\EXAMS01EXAMS01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.