string-parsing

Python. Join specific lines on 1 line

我怕爱的太早我们不能终老 提交于 2019-12-10 09:19:14
问题 Let's say I have this file: 1 17:02,111 Problem report related to router 2 17:05,223 Restarting the systems 3 18:02,444 Must erase hard disk now due to compromised data I want this output: 1 17:02,111 Problem report related to router 2 17:05,223 Restarting the systems 3 18:02,444 Must erase hard disk now due to compromised data Been trying in bash and got to a kind of close solution but I don't know how to carry this out on Python. Thank you in advance 回答1: If you want to remove the extea

Get contents after last slash

陌路散爱 提交于 2019-12-09 14:19:59
问题 I have strings that have a directory in the following format: C://hello//world How would i extract everything after the last / character (world)? 回答1: string path = "C://hello//world"; int pos = path.LastIndexOf("/") + 1; Console.WriteLine(path.Substring(pos, path.Length - pos)); // prints "world" The LastIndexOf method performs the same as IndexOf .. but from the end of the string. 回答2: using System.Linq; var s = "C://hello//world"; var last = s.Split('/').Last(); 回答3: There is a static

Parsing all possible types of varying architectural dimension input

安稳与你 提交于 2019-12-09 09:46:48
问题 I am writing a library for our company's product that will take any kind of architectural dimension that our users are already familiar with as input for a function that converts from a string to a double. Here is the list of input types that we would like to be valid. Input | Meaning | Output (Inches represented by a double) 12.5' | 12 Feet and six inches | 150.0 11" | 11 Inches | 11.0 3/16" | 3 sixteenths of an Inch | 0.1875 spaces may or may not be used between the feet and inches and the

DateTime.ParseExact - how to parse single- and double-digit hours with same format string?

自作多情 提交于 2019-12-08 16:35:37
问题 I want to be able to parse strings of time (hours, minutes, seconds) where the hours run from 0 to 23, and where the preceding zero for one-digit hours is optional. Examples of time strings that I want to be able to parse into valid DateTime objects: 212540 061525 94505 I am trying to use the C# method DateTime.ParseExact to manage the parsing, but I cannot for the life of it come up with a format string that can handle the "single-digit hour without preceding zero" scenario. How should I

Why does Integer.parseInt(“\uD835\uDFE8”) fail?

我怕爱的太早我们不能终老 提交于 2019-12-08 08:57:50
问题 I was under the impression that java supports unicode characters. I made this test and sadly found that it fails. The question is why? Is it a bug or somewhere documented? // MATHEMATICAL SANS-SERIF "𝟨" String unicodeNum6 = "\uD835\uDFE8"; int codePoint6 = unicodeNum6.codePointAt(0); int val6 = Character.getNumericValue(codePoint6); System.out.println("unicodeNum6 = "+ unicodeNum6 + ", codePoint6 = "+ codePoint6+ ", val6 = "+val6); int unicodeNum6Int = Integer.parseInt(unicodeNum6); This

How to avoid triggering an ArrayIndexOutOfBoundsException while parsing empty positions in a line of CSV?

血红的双手。 提交于 2019-12-07 14:16:26
String[] values = line.split(","); Long locId = Long.parseLong(replaceQuotes(values[0])); String country = replaceQuotes(values[1]); String region = replaceQuotes(values[2]); String city = replaceQuotes(values[3]); String postalCode = replaceQuotes(values[4]); String latitude = replaceQuotes(values[5]); String longitude = replaceQuotes(values[6]); String metroCode = replaceQuotes(values[7]); String areaCode = replaceQuotes(values[8]); //... public String replaceQuotes(String txt){ txt = txt.replaceAll("\"", ""); return txt; } I'm using the code above to parse a CSV with data in this format:

sscanf equivalent in Java [duplicate]

♀尐吖头ヾ 提交于 2019-12-07 07:47:43
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: what is the Java equivalent of sscanf for parsing values from a string using a known pattern? I'm pretty new to Java, but seeing how useful the sscanf function is, I wonder if there is an equivalent in Java. I've got many pretty much formatted strings (namely, writing scripting language for my purposes), so regular expressions are overkill here. 回答1: Scanner scanner = new Scanner ("2 A text 3 4"); scanner

How to remove unbalanced/unpartnered double quotes (in Java)

陌路散爱 提交于 2019-12-07 07:31:12
问题 I thought to share this relatively smart problem with everyone here. I am trying to remove unbalanced/unpaired double-quotes from a string. My work is in progress, I might be close to a solution. But, I didn't get a working solution yet. I am not able to delete the unpaired/unpartnered double-quotes from the string. Example Input string1=injunct! alter ego." string2=successor "alter ego" single employer" "proceeding "citation assets" Output Should be string1=injunct! alter ego. string2

Regex with non-capturing group in C#

走远了吗. 提交于 2019-12-06 18:26:37
问题 I am using the following Regex JOINTS.*\s*(?:(\d*\s*\S*\s*\S*\s*\S*)\r\n\s*)* on the following type of data: JOINTS DISPL.-X DISPL.-Y ROTATION 1 0.000000E+00 0.975415E+01 0.616921E+01 2 0.000000E+00 0.000000E+00 0.000000E+00 The idea is to extract two groups, each containing a line (starting with the Joint Number, 1, 2, etc.) The C# code is as follows: string jointPattern = @"JOINTS.*\s*(?:(\d*\s*\S*\s*\S*\s*\S*)\r\n\s*)*"; MatchCollection mc = Regex.Matches(outFileSection, jointPattern );

Extraction of some date formats failed when using Dateutil in Python

≯℡__Kan透↙ 提交于 2019-12-06 15:40:33
I have gone through multiple links before posting this question so please read through and below are the two answers which have solved 90% of my problem: parse multiple dates using dateutil How to parse multiple dates from a block of text in Python (or another language) Problem : I need to parse multiple dates in multiple formats in Python Solution by Above Links : I am able to do so but there are still certain formats which I am not able to do so. Formats which still can't be parsed are: text ='I want to visit from May 16-May 18' text ='I want to visit from May 16-18' text ='I want to visit