text-parsing

PHP - parsing a txt file

て烟熏妆下的殇ゞ 提交于 2019-11-26 17:30:21
问题 I have a .txt file that has the following details: ID^NAME^DESCRIPTION^IMAGES 123^test^Some text goes here^image_1.jpg,image_2.jpg 133^hello^some other test^image_3456.jpg,image_89.jpg What I'd like to do, is parse this ad get the values into a more readable format, possibly into an array if possible. Thanks 回答1: You can do that easily this way $txt_file = file_get_contents('path/to/file.txt'); $rows = explode("\n", $txt_file); array_shift($rows); foreach($rows as $row => $data) { //get row

Difference between parsing a text file in r and rb mode

岁酱吖の 提交于 2019-11-26 14:31:33
What makes parsing a text file in 'r' mode more convenient than parsing it in 'rb' mode? Especially when the text file in question may contain non-ASCII characters. lvc This depends a little bit on what version of Python you're using. In Python 2, Chris Drappier's answer applies. In Python 3, its a different (and more consistent) story: in text mode ( 'r' ), Python will parse the file according to the text encoding you give it (or, if you don't give one, a platform-dependent default), and read() will give you a str . In binary ( 'rb' ) mode, Python does not assume that the file contains things

How do I keep a Scanner from throwing exceptions when the wrong type is entered?

我与影子孤独终老i 提交于 2019-11-26 12:27:54
Here's some sample code: import java.util.Scanner; class In { public static void main (String[]arg) { Scanner in = new Scanner (System.in) ; System.out.println ("how many are invading?") ; int a = in.nextInt() ; System.out.println (a) ; } } If I run the program and give it an int like 4 , then everything goes fine. On the other hand, if I answer too many it doesn't laugh at my funny joke. Instead I get this(as expected): Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:819) at java.util.Scanner.next(Scanner.java:1431) at java.util.Scanner

Python parsing bracketed blocks

笑着哭i 提交于 2019-11-26 08:14:45
问题 What would be the best way in Python to parse out chunks of text contained in matching brackets? \"{ { a } { b } { { { c } } } }\" should initially return: [ \"{ a } { b } { { { c } } }\" ] putting that as an input should return: [ \"a\", \"b\", \"{ { c } }\" ] which should return: [ \"{ c }\" ] [ \"c\" ] [] 回答1: Pseudocode: For each string in the array: Find the first '{'. If there is none, leave that string alone. Init a counter to 0. For each character in the string: If you see a '{',

Evaluating a string of simple mathematical expressions [closed]

泪湿孤枕 提交于 2019-11-26 07:54:03
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Challenge Here is the challenge (of my own invention, though I wouldn\'t be surprised if it has previously appeared elsewhere on the web)

How do I keep a Scanner from throwing exceptions when the wrong type is entered?

依然范特西╮ 提交于 2019-11-26 02:57:52
问题 Here\'s some sample code: import java.util.Scanner; class In { public static void main (String[]arg) { Scanner in = new Scanner (System.in) ; System.out.println (\"how many are invading?\") ; int a = in.nextInt() ; System.out.println (a) ; } } If I run the program and give it an int like 4 , then everything goes fine. On the other hand, if I answer too many it doesn\'t laugh at my funny joke. Instead I get this(as expected): Exception in thread \"main\" java.util.InputMismatchException at

Split string containing command-line parameters into string[] in C#

℡╲_俬逩灬. 提交于 2019-11-26 01:43:07
问题 I have a single string that contains the command-line parameters to be passed to another executable and I need to extract the string[] containing the individual parameters in the same way that C# would if the commands had been specified on the command-line. The string[] will be used when executing another assemblies entry-point via reflection. Is there a standard function for this? Or is there a preferred method (regex?) for splitting the parameters correctly? It must handle \'\"\' delimited

Delete lines in a text file that contain a specific string

橙三吉。 提交于 2019-11-25 22:06:41
问题 How would I use sed to delete all lines in a text file that contain a specific string? 回答1: To remove the line and print the output to standard out: sed '/pattern to match/d' ./infile To directly modify the file – does not work with BSD sed: sed -i '/pattern to match/d' ./infile Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed: sed -i '' '/pattern to match/d' ./infile To directly modify the file (and create a backup) – works with BSD and GNU sed: sed -i.bak '/pattern

Split string containing command-line parameters into string[] in C#

孤人 提交于 2019-11-25 16:59:36
I have a single string that contains the command-line parameters to be passed to another executable and I need to extract the string[] containing the individual parameters in the same way that C# would if the commands had been specified on the command-line. The string[] will be used when executing another assemblies entry-point via reflection. Is there a standard function for this? Or is there a preferred method (regex?) for splitting the parameters correctly? It must handle '"' delimited strings that may contain spaces correctly, so I can't just split on ' '. Example string: string