Wanted to check if there is any null value in column field in the csv file and also there shouldn't be null value after / in number column, if it is null the entire row should not be written to output file.
name,number,gender,country
iva 1/001 f Antartica
aaju 2/002 m russia
lax 3/ m brazil
ana 4/004 f Thailand
vis 5/005 m
for e.g. 3rd and 5th row should not be written to output file.
using (StreamWriter file = new StreamWriter(filepathop)) {
for (int i = 0; i < csv.Length; i++) {
{
if (i == 0) {
file.WriteLine(header + "," + "num" + "," + "serial" + "," + "date");
}
else {
var newline = new StringBuilder();
string[] words = csv[i].Split(',');
string[] no = words[1].Split('/');
string number = no[0];
string serial = no[1];
newline.Append(number + "," + serial + "," + tokens[0]);
file.WriteLine(csv[i] + "," + newline);
}
}
}
}
}
}
}
You can test for null columns with string.IsNullOrEmpty(column) or column.Length == 0 like so:
if (!string.IsNullOrEmpty(serial) && !string.IsNullOrEmpty(country))
file.WriteLine(csv[i] + "," + newline);
You might want to check and remove white space, too. Depends on your input.
Related
\In my csv file have two row data :
\a;b;c;d;e;
\1;2;3;4;5;
\How do I make it as below in c#:
\a=1;enter code here
\b=2;enter code here
\c=3;enter code here
\d=4;enter code here
\e=5;enter code here
tldr; you split the text.
got string from csv //e.g. string originalTxt = #"\a;b;c;d;e; \1;2;3;4;5";
split row
rearrange/edit for each word in each row
combine
string resultTxt = "";
string mycodehere ="enter code here ";
string originalTxt = ...; //TODO :: your code for get text from csv
string[2] rows = originalTxt.Split('\'); // split the \ got 2 rows
string[] row1 = rows[0].Split(';'); //split each word
string[] row2 = rows[1].Split(';'); //split each word
for(int i= 0 ; i< row1.Count ; i++){
resultTxt += #"\" + row1[i] + "=" + row2[i] + ";" + mycodehere + " " ;
}
//TODO :: use resultTxt
After some image processing using ImgaeJ macro, I have a ‘Results’ tab which contains 2 columns A and B. Lets’s say I have 50 rows of data.
Now I want to subtract the value of last row from all other 49 rows above in column B.
After this, I want to write all the values in “.csv” file (column A, B and C with 49 values each).
Below is the part of the code. I think the only problem is fetching the values from arrays that the script can write to the csv file.
Array.getStatistics command only exports the mean, std values for a given column. I'm interested in fetching all 49 values.
directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";
A = newArray(nResults() - 1);
B = newArray(nResults() - 1);
D = getResult("B", nResults() - 1);
for (i = 0; i < nResults() - 2; i++) {
A[i] = getResult("A", i);
B[i] = getResult("B", i);
C[i] = A[i] - D;
}
Any idea about what is the command to get the values of A[i], B[i] and C[i]?
Looking forward for some help here.
Thank you.
One solution is to write to the file as you do the calculations. I have modified your example (untested) to show how this works.
directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";
f = File.open(resultFilename);
A = newArray(nResults() - 1);
B = newArray(nResults() - 1);
// no C array is made so:
C = newArray(nResults() - 1);
D = getResult("B", nResults() - 1);
for (i = 0; i < nResults() - 2; i++) {
A[i] = getResult("A", i);
B[i] = getResult("B", i);
C[i] = A[i] - D;
// should the line above should be C[i] = B[i] - D;
print(f, d2s(A[i],6) + " \t" + d2s(B[i],6) + " \t" + d2s(C[i],6));
}
File.close(f);
Note that you don't need to make the arrays at all and can just write to the file (again this is untested):
directory = getDirectory("Choose a Directory");
resultFilename = directory + Dialog.getString() + ".csv";
f = File.open(resultFilename);
D = getResult("B", nResults() - 1);
for (i = 0; i < nResults() - 2; i++) {
ai = getResult("A", i);
bi = getResult("B", i);
ci = ai - D;
// should the line above should be ci = bi - D;
print(f, d2s(ai,6) + " \t" + d2s(bi,6) + " \t" + d2s(ci,6));
}
File.close(f);
I have used " \t" (tab character) as a separator not comma.
How we can set up a function on node
After first two number and last number, we want to add - hyphen
able to get a string
like this:xxzzzzzzzzx
and convert in to this:xx-zzzzzzzz-x
example of what we need
function tranformer (xxzzzzzzzzx){
NOT SURE HOW TO SOLVE THIS
return xx-zzzzzzzz-x
}
Thanks we really will appreciate this!
Not idea how to mange this task.
function tranformer(st) {
let newStr = ""
for (let i = 0; i < st.length; i++) {
if (i === 2 || i === st.length - 1) {
newStr += "-"
}
newStr += st[i]
}
return newStr
}
Using Slice
let first = st.slice(0, 2)
let middle = st.slice(2, -1)
let last = st.slice(-1)
newStr = first + "-" + middle + "-" + last
console.log(newStr)
First I would decompose the string into an array, and then use splice command to insert a - char at the specified position:
let str = "xxzzzzzzzzx";
str = str.split('');
str.splice(2, 0, '-'); // insert - at pos 2
str.splice(str.length - 1, 0, '-'); // insert - at pos -1
console.log(str.join('')) //-> xx-zzzzzzzz-x
Trying to read from a data file with number of rows of data and each row the number of elements are varying.
StreamReader read = new StreamReader("TextFile1.txt");
string str1 = " ";
while (str1 != null)
{
str1 = read.ReadLine();
if (str1 != null)
{
richTextBox1.AppendText("\n"+str1);
string[] s = str1.Split(' ');
i = 0;
sum = 0;
while (s[i] != null)
{
if(i>0)
j=int.Parse(s[i]);
sum = sum + j;
i = i + 1;
}
}
}
Presuming the code you posted is C#, you should use the String.length property.
Just a snippet of that from the inside of your loop:
string[] s = str1.Split(' ');
i = 0;
sum = 0;
while (i < s.length)
{
j=int.Parse(s[i]);
sum = sum + j;
i = i + 1;
}
By the way, I removed the if(i > 0) condition as it seems rather unnecessary. However, if you would like to exclude the first element on the line (which is what the if(i>0) condition was doing), then simply change i = 0; to i = 1;.
I am able to read the table cells, But I wanted also to read the applied style name of each cell of a row in a table. How can I achieve this?
EDIT
Following is the code snip which I have tried. By this I am able to read cell text also the applied pstyle(para style), but not able to read the rstyles.
private static void processDoc(String path) throws Exception {
POIFSFileSystem fis = new POIFSFileSystem(new FileInputStream(path));
HWPFDocument wdDoc = new HWPFDocument(fis);
// list all style names and indexes in stylesheet
/*for (int j = 0; j < wdDoc.getStyleSheet().numStyles(); j++) {
if (wdDoc.getStyleSheet().getStyleDescription(j) != null) {
System.out.println(j + ": " + wdDoc.getStyleSheet().getStyleDescription(j).getName());
} else {
// getStyleDescription returned null
System.out.println(j + ": " + null);
}
}*/
// set range for entire document
Range range = wdDoc.getRange();
for (int i = 0; i < range.numParagraphs(); i++) {
Paragraph p = range.getParagraph(i);
// check if style index is greater than total number of styles
if (wdDoc.getStyleSheet().numStyles() > p.getStyleIndex()) {
//System.out.println(wdDoc.getStyleSheet().numStyles() + " -> " + p.getStyleIndex());
StyleDescription style = wdDoc.getStyleSheet().getStyleDescription(p.getStyleIndex());
String styleName = style.getName();
// write style name and associated text
System.out.println(styleName + " -> " + p.text().replaceAll("[\u0000-\u001f]", ""));
} else {
System.out.println("\n" + wdDoc.getStyleSheet().numStyles() + " ----> " + p.getStyleIndex());
}
}
}