python replace single backslash with double backslash [duplicate]

No need to use str.replace or string.replace here, just convert that string to a raw string: >>> strs = r”C:\Users\Josh\Desktop\20130216″ ^ | notice the ‘r’ Below is the repr version of the above string, that’s why you’re seeing \\ here. But, in fact the actual string contains just ‘\’ not \\. >>> strs ‘C:\\Users\\Josh\\Desktop\\20130216’ >>> … Read more

Replace all occurrences of a string in a pandas dataframe (Python)

You can use replace and pass the strings to find/replace as dictionary keys/items: df.replace({‘\n’: ‘<br>’}, regex=True) For example: >>> df = pd.DataFrame({‘a’: [‘1\n’, ‘2\n’, ‘3’], ‘b’: [‘4\n’, ‘5’, ‘6\n’]}) >>> df a b 0 1\n 4\n 1 2\n 5 2 3 6\n >>> df.replace({‘\n’: ‘<br>’}, regex=True) a b 0 1<br> 4<br> 1 2<br> 5 2 … Read more

Find and replace words/lines in a file

Any decent text editor has a search&replace facility that supports regular expressions. If however, you have reason to reinvent the wheel in Java, you can do: Path path = Paths.get(“test.txt”); Charset charset = StandardCharsets.UTF_8; String content = new String(Files.readAllBytes(path), charset); content = content.replaceAll(“foo”, “bar”); Files.write(path, content.getBytes(charset)); This only works for Java 7 or newer. If … Read more

How to replace tabs with spaces in Atom?

Atom has a built-in tool for this Activate the command palette (ShiftCmdP on Mac, CtrlShiftP on Windows/Linux) and search for “convert space” or “convert tab”. You should find these three commands are available: Whitespace: Convert Spaces to Tabs Whitespace: Convert Tabs to Spaces Whitespace: Convert All Tabs to Spaces Convert Tabs vs. Convert All Tabs … Read more

Remove all text before colon

Here are two ways of doing it in R: foo <- “TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.adj:PKMYT1” # Remove all before and up to “:”: gsub(“.*:”,””,foo) # Extract everything behind “:”: regmatches(foo,gregexpr(“(?<=:).*”,foo,perl=TRUE))