Skip to content

Instantly share code, notes, and snippets.

@rjlutz
Last active September 21, 2017 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjlutz/c54263c53113f23a368507873601c458 to your computer and use it in GitHub Desktop.
Save rjlutz/c54263c53113f23a368507873601c458 to your computer and use it in GitHub Desktop.
File and Web IO Examples
<!-- example of very simple html file -->
<html>
<h1>Consolidated ASCII Table</h1>
<table border="3" cellpadding="3" cellspacing='3'>
<thead>
<tr><th>
<p><strong>Decimal</strong></p>
</th><th>
<p><strong>Character</strong></p>
</th>
</tr>
</thead>
<tbody>
<tr><td>
<p>65</p>
</td><td align="right">
<p>A</p>
</td>
</tr>
<tr><td>
<p>66</p>
</td><td align="right">
<p>B</p>
</td>
</tr>
<tr><td>
<p>67</p>
</td><td align="right">
<p>C</p>
</td>
</tr>
<tr><td>
<p>68</p>
</td><td align="right">
<p>D</p>
</td>
</tr>
<tr><td>
<p>69</p>
</td><td align="right">
<p>E</p>
</td>
</tr>
<tr><td>
<p>70</p>
</td><td align="right">
<p>F</p>
</td>
</tr>
<tr><td>
<p>71</p>
</td><td align="right">
<p>G</p>
</td>
</tr>
<tr><td>
<p>72</p>
</td><td align="right">
<p>H</p>
</td>
</tr>
<tr><td>
<p>73</p>
</td><td align="right">
<p>I</p>
</td>
</tr>
<tr><td>
<p>74</p>
</td><td align="right">
<p>J</p>
</td>
</tr>
<tr><td>
<p>75</p>
</td><td align="right">
<p>K</p>
</td>
</tr>
<tr><td>
<p>76</p>
</td><td align="right">
<p>L</p>
</td>
</tr>
<tr><td>
<p>77</p>
</td><td align="right">
<p>M</p>
</td>
</tr>
<tr><td>
<p>78</p>
</td><td align="right">
<p>N</p>
</td>
</tr>
<tr><td>
<p>79</p>
</td><td align="right">
<p>O</p>
</td>
</tr>
<tr><td>
<p>80</p>
</td><td align="right">
<p>P</p>
</td>
</tr>
<tr><td>
<p>81</p>
</td><td align="right">
<p>Q</p>
</td>
</tr>
<tr><td>
<p>82</p>
</td><td align="right">
<p>R</p>
</td>
</tr>
<tr><td>
<p>83</p>
</td><td align="right">
<p>S</p>
</td>
</tr>
<tr><td>
<p>84</p>
</td><td align="right">
<p>T</p>
</td>
</tr>
<tr><td>
<p>85</p>
</td><td align="right">
<p>U</p>
</td>
</tr>
<tr><td>
<p>86</p>
</td><td align="right">
<p>V</p>
</td>
</tr>
<tr><td>
<p>87</p>
</td><td align="right">
<p>W</p>
</td>
</tr>
<tr><td>
<p>88</p>
</td><td align="right">
<p>X</p>
</td>
</tr>
<tr><td>
<p>89</p>
</td><td align="right">
<p>Y</p>
</td>
</tr>
<tr><td>
<p>90</p>
</td><td align="right">
<p>Z</p>
</td>
</tr>
</tbody>
</table>
</html>
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 7. in line 1.
# excerpt from longer file
"c_image", "l_image", "r_image", "steering", "throttle", "brake", "speed"
center_2017_09_10_19_06_00_906.jpg,left_2017_09_10_19_06_00_906.jpg,right_2017_09_10_19_06_00_906.jpg,0,0.4,0,2.351806
center_2017_09_10_19_06_00_980.jpg,left_2017_09_10_19_06_00_980.jpg,right_2017_09_10_19_06_00_980.jpg,0,0.4,0,2.713444
center_2017_09_10_19_06_01_051.jpg,left_2017_09_10_19_06_01_051.jpg,right_2017_09_10_19_06_01_051.jpg,0,0.4,0,2.986505
center_2017_09_10_19_06_01_118.jpg,left_2017_09_10_19_06_01_118.jpg,right_2017_09_10_19_06_01_118.jpg,0,0.4,0,3.357141
center_2017_09_10_19_06_01_197.jpg,left_2017_09_10_19_06_01_197.jpg,right_2017_09_10_19_06_01_197.jpg,0,0.4,0,3.719582
center_2017_09_10_19_06_01_268.jpg,left_2017_09_10_19_06_01_268.jpg,right_2017_09_10_19_06_01_268.jpg,0,0.4,0,3.987639
center_2017_09_10_19_06_01_337.jpg,left_2017_09_10_19_06_01_337.jpg,right_2017_09_10_19_06_01_337.jpg,0,0.4,0,4.342322
center_2017_09_10_19_06_01_404.jpg,left_2017_09_10_19_06_01_404.jpg,right_2017_09_10_19_06_01_404.jpg,0,0.4,0,4.6061
center_2017_09_10_19_06_01_483.jpg,left_2017_09_10_19_06_01_483.jpg,right_2017_09_10_19_06_01_483.jpg,0,0.4,0,4.956964
center_2017_09_10_19_06_01_552.jpg,left_2017_09_10_19_06_01_552.jpg,right_2017_09_10_19_06_01_552.jpg,0,0.4,0,5.217943
center_2017_09_10_19_06_01_630.jpg,left_2017_09_10_19_06_01_630.jpg,right_2017_09_10_19_06_01_630.jpg,0,0.4,0,5.563562
center_2017_09_10_19_06_01_698.jpg,left_2017_09_10_19_06_01_698.jpg,right_2017_09_10_19_06_01_698.jpg,0,0.4,0,5.906468
center_2017_09_10_19_06_01_768.jpg,left_2017_09_10_19_06_01_768.jpg,right_2017_09_10_19_06_01_768.jpg,0,0.4,0,6.161858
center_2017_09_10_19_06_01_836.jpg,left_2017_09_10_19_06_01_836.jpg,right_2017_09_10_19_06_01_836.jpg,0,0.4,0,6.500007
center_2017_09_10_19_06_01_914.jpg,left_2017_09_10_19_06_01_914.jpg,right_2017_09_10_19_06_01_914.jpg,0,0.4,0,6.835454
center_2017_09_10_19_06_01_981.jpg,left_2017_09_10_19_06_01_981.jpg,right_2017_09_10_19_06_01_981.jpg,0,0.4,0,7.085277
center_2017_09_10_19_06_02_052.jpg,left_2017_09_10_19_06_02_052.jpg,right_2017_09_10_19_06_02_052.jpg,0,0.4,0,7.333351
center_2017_09_10_19_06_02_130.jpg,left_2017_09_10_19_06_02_130.jpg,right_2017_09_10_19_06_02_130.jpg,0,0.4,0,7.660692
center_2017_09_10_19_06_02_198.jpg,left_2017_09_10_19_06_02_198.jpg,right_2017_09_10_19_06_02_198.jpg,0,0.4,0,7.985483
center_2017_09_10_19_06_02_268.jpg,left_2017_09_10_19_06_02_268.jpg,right_2017_09_10_19_06_02_268.jpg,0,0.4,0,8.227386
center_2017_09_10_19_06_02_336.jpg,left_2017_09_10_19_06_02_336.jpg,right_2017_09_10_19_06_02_336.jpg,0,0.4,0,8.547668
center_2017_09_10_19_06_02_415.jpg,left_2017_09_10_19_06_02_415.jpg,right_2017_09_10_19_06_02_415.jpg,0,0.4,0,8.865382
center_2017_09_10_19_06_02_485.jpg,left_2017_09_10_19_06_02_485.jpg,right_2017_09_10_19_06_02_485.jpg,0,0.4,0,9.101989
center_2017_09_10_19_06_02_552.jpg,left_2017_09_10_19_06_02_552.jpg,right_2017_09_10_19_06_02_552.jpg,0,0.4,0,9.337178
center_2017_09_10_19_06_02_619.jpg,left_2017_09_10_19_06_02_619.jpg,right_2017_09_10_19_06_02_619.jpg,0,0.4,0,9.648559
...
...
2500 more lines for 3 minutes of recording on simple sim, 3 cameras at 320x160x3, full file paths pruned for readabilty ....
P3
3 2
255
# The part above is the header
# "P3" means this is a RGB color image in ASCII
# "3 2" is the width and height of the image in pixels
# "255" is the maximum value for each color
# The part below is image data: RGB triplets
#
# taken from https://en.wikipedia.org/wiki/Netpbm_format, lots more info there
#
255 0 0 0 255 0 0 0 255
255 255 0 255 255 255 0 0 0
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Scanner;
public class FileOutputWebInputYahooStockReport {
public static void main(String[] args) throws IOException {
System.out.println("Starting ...");
// open / get reference to my input and output file
// File inputFile = new File("quotes.csv");
// Scanner input = null;
URL url = new URL("http://download.finance.yahoo.com"
+ "/d/quotes.csv?s=BAC,SBUX,FCX,NVDA&f=sl1d1t1c1ohgv&e=.csv");
Scanner input = null;
File outputFile = new File("report.txt");
PrintWriter output = null;
try {
input = new Scanner(url.openStream());
output = new PrintWriter(outputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("Error reading file");
System.exit(0);
}
// read each line, extract ticker and the price,
// print to the report file
while(input.hasNext()) {
String line = input.nextLine();
String[] tokens = line.split(",");
output.println(tokens[0].substring(1, tokens[0].length()-1) + " " + tokens[1]);
}
input.close();
output.close();
System.out.println("Done.");
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class FIleOutputYahooStockReport {
public static void main(String[] args) {
System.out.println("Starting ...");
// open / get reference to my input and output file
File inputFile = new File("quotes.csv");
Scanner input = null;
File outputFile = new File("report.txt");
PrintWriter output = null;
try {
input = new Scanner(inputFile);
output = new PrintWriter(outputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("Error reading file");
System.exit(0);
}
// read each line, extract ticker and the price,
// print to the report file
while(input.hasNext()) {
String line = input.nextLine();
String[] tokens = line.split(",");
output.println(tokens[0].substring(1, tokens[0].length()-1) + " " + tokens[1]);
}
input.close();
output.close();
System.out.println("Done.");
}
}
FB 92.77 9/25/2015 4:00pm -1.64 95.85 95.85 92.06 28961622
FTR 5.00 9/25/2015 4:00pm +0.17 4.85 5.11 4.85 28553188
MU 14.91 9/25/2015 4:00pm -0.43 15.50 15.59 14.65 28917267
MSFT 43.94 9/25/2015 4:00pm +0.03 44.59 44.73 43.76 29384601
ORCL 36.05 9/25/2015 4:01pm -0.01 36.20 36.32 35.91 19386998
CSCO 26.025 9/25/2015 4:00pm +0.615 25.850 26.295 25.800 37618273
AAL 40.24 9/25/2015 4:00pm -0.01 40.89 41.03 39.92 7524947
AAL 40.24 9/25/2015 4:00pm -0.01 40.89 41.03 39.92 7524947
SONC 24.53 9/25/2015 4:00pm -0.03 24.81 25.05 24.44 992608
NVDA 23.61 9/25/2015 4:00pm +0.17 23.73 24.07 23.38 9242268
SPSL N/A N/A N/A N/A N/A N/A N/A N/A
AAPL 114.71 9/25/2015 4:00pm -0.29 116.36 116.69 114.02 56151926
IBM 145.42 9/25/2015 4:01pm +1.01 145.55 146.27 144.53 3474429
http://download.finance.yahoo.com/d/quotes.csv?s=BAC,FCX,NVDA&f=sl1d1t1c1ohgv&e=.csv
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SOPYahooStockReport {
public static void main(String[] args) {
// open / get reference to my input file
File inputFile = new File("quotes.csv");
Scanner input = null;
try {
input = new Scanner(inputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("Error reading file");
System.exit(0);
}
// read each line, print to console
while(input.hasNext()) {
String line = input.nextLine();
String[] tokens = line.split(",");
//System.out.println("line = " + line);
System.out.println(tokens[0].substring(1, tokens[0].length()-1) + " " + tokens[1]);
}
// extract ticker and the price
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class WritingExperiment {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Starting ...");
File file = new File("ascii-report.html");
PrintWriter writer = new PrintWriter(file);
// write all the header
writer.println("<html>");
writer.println("<h1>Consolidated ASCII Table</h1>");
writer.println("<table border=\"3\" cellpadding=\"3\" cellspacing='3'>");
writer.println("<thead>");
writer.println("<tr><th>");
writer.println("<p><strong>Decimal</strong></p>");
writer.println("</th><th>");
writer.println("<p><strong>Character</strong></p>");
writer.println("</th>");
writer.println("</tr>");
writer.println("</thead>");
writer.println("<tbody>");
// loop for processing chars from 65 to 90
for (int i = 65; i <=90; i++)
writer.println("<tr><td><p>" + i + "</p></td><td align=\"right\"><p>" +
(char)i + "</p></td></tr>");
// write all the footer stuff
writer.println("</tbody>");
writer.println("</table>");
writer.println("</html>");
writer.close();
System.out.println("Done.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment