mercredi 1 avril 2015

Python putting r before unicode string variable

For static strings, putting an r in front of the string would give the raw string (e.g. r'some \' string'). Since it is not possible to put r in front of a string variable, what is the minimal approach to dynamically convert a string variable to its raw form? Should I manually substitute all backslashes with double backslashes?



str_var = u"some text with escapes e.g. \( \' \)"
raw_str_var = ???

Insert string into specific part of another string

I've got a URL as a string, for example:



http://ift.tt/1Dout16


I'd like to add another subfolder to it with PHP, before hello, so it should look like this:



http://ift.tt/1C784y4


I thought about using explode to separate the URL by slashes, and adding another one before the last one, but I'm pretty sure I over complicate it. Is there an easier way?


SQL - Query to find if a string contains part of the value in Column

I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string). Say for example I have a column in a table with values ABC,XYZ. If I give search string ABCDEFG then I want the row with ABC to be displayed. If my search string is XYZDSDS then the row with value XYZ should be displayed


Using something else instead of String

I have a big file and I want to do some „operations” on it.(find some text, check if some text exists, get the offset of some text, maybe changing the file).


My current aproach is this:



public ResultSet getResultSet(String fileName) throws IOException {

InputStream in = new FileInputStream(fileName);

byte[] buffer = new byte[CAPACITY];
byte[] doubleBuffer = new byte[2 * CAPACITY];


long len = in.read(doubleBuffer);
while (true) {
String reconstitutedString = new String(doubleBuffer, 0 ,doubleBuffer.length);

//...do stuff

ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(doubleBuffer, CAPACITY, CAPACITY);
readUntilNow += len;
len = in.read(buffer);
if (len <= 0) {
break;
}
os.write(buffer, 0, CAPACITY);
doubleBuffer = os.toByteArray();
os.close();
}
in.close();
return makeResult();

}


I would like to change the String reconstitutedString into something else. What would be the best alternative considering I want to be able to get some information about the content of that data, information that I may get calling an IndexOf on a String


Extracting a specific part of a string

I have this string:



[25-03-15, 1236], [26-03-15, 3000], [27-03-15, 3054], [30-03-15, 4000]


I want to get two parts from it as below:



['25-03-15','26-03-15','27-03-15','30-03-2015']


and



[1236,3000,3054,4000]


Please guide me how I can perform this task.


PS: I am working of view page of codeigniter.


I'm getting the first thing as:



<?php
$usd=$this->db->query('select transaction_date, SUM(amount) as total
from transactions GROUP BY transaction_date')->result_array();
$str = '';

for($i=0; $i<count($usd); $i++){
if($i!=0){
$str = $str.', ['.date('d-m-y', strtotime($usd[$i]["transaction_date"])).', '.$usd[$i]["total"].']';
}else{
$str = $str.'['.date('d-m-y', strtotime($usd[$i]["transaction_date"])).', '.$usd[$i]["total"].']';
}
}

echo $str;

?>

Recursion - WHY does this print the output backwards?

Can somebody explain why this program outputs ZYx321cBa instead of: aBc123xYZ


I dont seem to understand why. I notice when the recursion is called at the end of the method, it prints properly, but not when recursion is called first.



public class R {

public static void main(String[] args) {

String str ="aBc123xYZ";

Rc rev = new Rc(str);
rev.raC(0);
}
}


Second class, that prints the string based on the index entered in previous class



class Rc {

String s;
int lc = 0;

Rc(String s) {
this.s = s;
}

void raC(int index) {
if (index != s.length()) {
raC(index + 1);
//System.out.println(index);
System.out.println(s.charAt(index) + " ");
if(Character.isLowerCase(s.charAt(index))) {
lc ++;
}
}

if (index == 0) {
System.out.println(":" + lc);
}
}
}

Reading and writing String files with JSON in java

I want to read a text file and process it's data.so for example the input file will look like :



john,judd,134
Kaufman,kim,345


then the program should parse and store these data in form of a JSON file so it will be organized for further processing.I'm using JSON-simple for this task .and this is a prototype code I've written:



package com.company;

import org.json.simple.JSONObject;

import java.io.*;

public class Main {


static JSONObject jsonObject = new JSONObject();
static String output;

public static void main(String[] args) throws IOException {

read("/Users/Sepehr/Desktop/JSONexample.txt");
write("/Users/Sepehr/Desktop/JSONexampleout,txt");

}

public static String read(String filenameIn) throws IOException {

BufferedReader bufferedReader = new BufferedReader(new FileReader(filenameIn));
String s ;

while ( (s = bufferedReader.readLine() ) != null)

{
String[] stringsArr = s.split(",");


jsonObject.put( "famname" , stringsArr[0] );
jsonObject.put("name" , stringsArr[1]);
jsonObject.put("id", stringsArr[2]);

bufferedReader.close();


}

return output=jsonObject.toJSONString();


}


public static String write(String filenameOut) throws FileNotFoundException {

PrintWriter printWriter = new PrintWriter(filenameOut);
printWriter.write(jsonObject.toJSONString());
printWriter.close();

String se = "yaaayyy :|";
return se;

}



}


after running the program these are the exceptions I get:



Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(BufferedReader.java:97)
at java.io.BufferedReader.readLine(BufferedReader.java:292)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at com.company.Main.read(Main.java:30)
at com.company.Main.main(Main.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


what is wrong exactly ?


and how should make a better design for this program ?