fs.readFileSync adding + signs? - node.js
I am working on an HTTP fileserver and stumbled upon a problem with fs.readFileSync(). However, the function adds + signs which prohibits me from writing the value to another value.
(This is just to show what happens and does not represent the full functionality of the intended code)
samplejs.js
var content = fs.readFileSync("D:/sampleproj/example.txt", {encoding:"utf8"});
console.log(content);
output:
"\t'#0000CC',\r" +
"\n\t'#0000FF',\r" +
"\n\t'#0033CC',\r" +
"\n\t'#0033FF',\r" +
"\n\t'#0066CC',\r" +
"\n\t'#0066FF',\r" +
"\n\t'#0099CC',\r" +
"\n\t'#0099FF',\r" +
"\n\t'#00CC00',\r" +
"\n\t'#00CC33',\r" +
"\n\t'#00CC66',\r" +
"\n\t'#00CC99',\r" +
"\n\t'#00CCCC',\r" +
"\n\t'#00CCFF',\r" +
"\n\t'#3300CC',\r" +
"\n\t'#3300FF',\r" +
"\n\t'#3333CC',\r" +
"\n\t'#3333FF',\r" +
"\n\t'#3366CC',\r" +
"\n\t'#3366FF',\r" +
"\n\t'#3399CC',\r" +
"\n\t'#3399FF',\r" +
"\n\t'#33CC00',\r" +
"\n\t'#33CC33',\r" +
"\n\t'#33CC66',\r" +
"\n\t'#33CC99',\r" +
"\n\t'#33CCCC',\r" +
"\n\t'#33CCFF',\r" +
"\n\t'#6600CC',\r" +
"\n\t'#6600FF',\r" +
"\n\t'#6633CC',\r" +
"\n\t'#6633FF',\r" +
"\n\t'#66CC00',\r" +
The problem here is, of course, the + signs. How can these be "avoided"/removed?
I've tried to .split("+") but that doesn't work since the + signs aren't inside a pair of quotes.
the text file:
'#0000CC',
'#0000FF',
'#0033CC',
'#0033FF',
'#0066CC',
'#0066FF',
'#0099CC',
'#0099FF',
'#00CC00',
'#00CC33',
'#00CC66',
'#00CC99',
'#00CCCC',
'#00CCFF',
'#3300CC',
'#3300FF',
'#3333CC',
'#3333FF',
'#3366CC',
'#3366FF',
'#3399CC',
'#3399FF',
'#33CC00',
'#33CC33',
'#33CC66',
'#33CC99',
'#33CCCC',
'#33CCFF',
'#6600CC',
'#6600FF',
'#6633CC',
'#6633FF',
'#66CC00',
'#66CC33',
'#9900CC',
'#9900FF',
'#9933CC',
'#9933FF',
'#99CC00',
'#99CC33',
'#CC0000',
'#CC0033',
'#CC0066',
'#CC0099',
'#CC00CC',
Are you sure you are doing console.log?
I did console.log and didn't get any "+", but I got those when I use console.dir.
Anyway, these "+", are not part of the string and you can't remove them by using split.
And just use the string as if they were not there.
To get each line simply use:
const fs = require("fs");
let file = fs.readFileSync(__dirname + "/text.txt", { encoding: "utf8" });
const lines = file.split("\n");
Related
How to set split() start and end delimiter?
[(0, '0.011*"people" + 0.009*"christian" + 0.008*"god" + 0.008*"law" + ' '0.006*"believe" + 0.005*"question" + 0.005*"man" + 0.005*"life" + ' '0.005*"time" + 0.005*"write"'), (1, '0.014*"organization" + 0.013*"group" + 0.012*"image" + 0.010*"university" + ' '0.009*"program" + 0.008*"newsletter" + 0.007*"graphic" + ' '0.007*"information" + 0.007*"file" + 0.006*"box"'), (2, '0.015*"write" + 0.015*"organization" + 0.014*"article" + 0.012*"year" + ' '0.008*"university" + 0.007*"team" + 0.007*"time" + 0.006*"game" + ' '0.006*"give" + 0.006*"kid"'), (3, '0.049*"space" + 0.009*"year" + 0.008*"publish" + 0.006*"aerospace" + ' '0.006*"news" + 0.006*"technical" + 0.005*"satellite" + 0.005*"activity" + ' '0.005*"membership" + 0.005*"system"')] How do I set the delimeter for the text file shown in the image? I want it to split into four separate text files. What and how should I give the start and end delimiter in the if() as can be seen in the code?. The text file has four separate parts 0,1,2,3. I am trying to write all the parts into separate text file. `with open('topics.txt','r') as fo: op='' start=0 cntr = 1 for x in fo.read().split("\n"): if (x==''): if (start==1): with open(str(cntr) + '.txt','w') as opf: opf.write(op) opf.close() op='' cntr+=1 else: start=1 else: if (op==''): op = x else: op = op + '\n' + x fo.close()`
If what you posted above is literally your text file, then this should give you each tuple separately. I'm just using the regular expressions library. The pattern is just look for a left paren ( and run of anything that isn't a right paren, and then a right paren. Super simple. import re foo = """[(0,\n blahblahblah), (1,\n asdfasdf), (2,\n ghhgghiegiegieh)]""" pat = r'\([^\)]+\)' matches = re.findall(pat, foo) ['(0,\n blahblahblah)', '(1,\n asdfasdf)', '(2,\n ghhgghiegiegieh)'] If you want to separate out the numbers you can do that easily by spliting and striping out the extra stuff: [i[1:-1].split(',\n')[1].strip() for i in matches] #['blahblahblah', 'asdfasdf', 'ghhgghiegiegieh'] Then you can write them to whatever file you like.
Nodejs, asny to sync
I am quite new to NodeJs and need some help regarding flow. So, I need to change some lib as it does not work for me. I have asny call and then right a way i have sync code. The thing is that sync code starts executing before asny part return data. asny: xmlenc.encrypt(message, options509, function(err, result) { console.log("error:", err) message = result return message; }) sync right after asyn: xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<" + envelopeKey + ":Envelope " + xmlnsSoap + " " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + encoding + this.wsdl.xmlnsInEnvelope + '>' + ((self.soapHeaders || self.security) ? ( "<" + envelopeKey + ":Header>" + (self.soapHeaders ? self.soapHeaders.join("\n") : "") + (self.security && !self.security.postProcess ? self.security.toXML() : "") + "</" + envelopeKey + ":Header>" ) : '' ) + "<" + envelopeKey + ":Body" + (self.bodyAttributes ? self.bodyAttributes.join(' ') : '') + (self.security && self.security.postProcess ? ' Id="_0"' : '') + ">" + genXML() + "</" + envelopeKey + ":Body>" + "</" + envelopeKey + ":Envelope>"; How to solve this that the progrem with wait for asyn part to return before moving to sync part. thank you
Vue bind SVG transform rotate
Trying to rotate an svg group by an amount deg in data():{ deg: 90, groupCenter: [100,200] } I have searched but can't find the correct syntax to bind data to the svg rotate function, :transform="{'rotate(' + deg + ' ' + groupCenter[0] + ' ' + groupCenter[1] + ')'}" I am trying to add this to a circle like this, <circle style="mix-blend-mode: multiply;" v-for="(el,index) in element.coords" :fill="el.color" :key="index" :r="el.radius" :cy="el.y" :cx="el.x" :transform="{'rotate(' + deg + ' ' + groupCenter[0] + ' ' + groupCenter[1] + ')'}" /> I get, - invalid expression: Unexpected token + in {'rotate(' + deg + ' ' + groupCenter[0] + ' ' + groupCenter[1] + ')'} Raw expression: :transform="{'rotate(' + deg + ' ' + groupCenter[0] + ' ' + groupCenter[1] + ')'}" SOLUTION: This worked for me using a method, :transform="rotateShape(index)" And the method, rotateShape(){ return "rotate(" + this.deg + " 0 0)" }, But I don't know why the initial attempt wouldn't work.
The transform attribute needs to evaluate to a string - i.e. transform="rotate(90deg)", so I think you need to concatenate some partial strings: :transform="'rotate(' + deg + ' deg)'"
This is a great place to use javascript's template literals. :transform="`rotate(${deg} deg)`"
Problematic formula with Apache POI
I'm using Java and generating an Excel file via Apache POI. The following formula works perfect. cell4.setCellFormula('(J' + (itemCountSize + 3) + '-H' + (itemCountSize + 3)+ ')') Now I simply want to divide by the same H value eg. (J50-H50)/H50 cell4.setCellFormula('(J' + (itemCountSize + 3) + '-H' + (itemCountSize + 3)+ ')'+ '/H' + (itemCountSize + 3)) However it still just gives me (J50-H50)... Any assistance would be appreciated.
Think you need an extra enclosing bracket Have you tried: cell4.setCellFormula('((J' + (itemCountSize + 3) + '-H' + (itemCountSize + 3)+ ')'+ '/H' + (itemCountSize + 3) + ')') Or, simplifying with groovy string templating: cell4.cellFormula = "((J${itemCountSize+3}-H${itemCountSize+3})/H${itemCountSize+3})" –
#DocuSignAPI unable to fetch Title in captive signing
I have a document that requires signing by the clients, and the clients should be able to change their name and title. I dragged and dropped the name field which is getting populated from the customize signature process and similarly I added the title field and hoped it would do the same. However I tried the following it always turns up blank. I am not able to prefill it using the following: body = "<envelopeDefinition xmlns=\"http://www.docusign.com/restapi\">" + "<emailSubject>API Call for adding signature request to document and sending</emailSubject>" + "<status>sent</status>" + "<documents>" + "<document>" + "<documentId>"+documentId+"</documentId>" + "<name>" + documentName + "</name>" + "</document>" + "</documents>" + "<recipients>" + "<signers>" + "<signer>" + "<recipientId>1</recipientId>" + "<name>" + recipientName + "</name>" + "<email>" + recipientEmail + "</email>" + "<clientUserId>" + planId + "</clientUserId>" + "<title>CFO</title>" << 3rd Attempt + "<tabs>" + "<fullNameTabs>" + "<fullName>" // working perfectly + "<xPosition>" + nameXPos + "</xPosition>" + "<yPosition>" + nameYPos + "</yPosition>" + "<documentId>"+documentId+"</documentId>" + "<pageNumber>" + namePage + "</pageNumber>" + "</fullName>" + "</fullNameTabs>" + "<titleTabs>" + "<title>" // not pre-filling text + "<xPosition>" + titleXPos + "</xPosition>" + "<yPosition>" + titleYPos + "</yPosition>" + "<documentId>"+documentId+"</documentId>" + "<pageNumber>" + titlePage + "</pageNumber>" + "<name>GM</name><width>200</width>" << Tooltip works + "<value>General Manager</value>" << Not working (bug?) + "<tabLabel>General Manager</tabLabel>" << 2nd attempt + "<required>false</required>" + "</title>" + "</titleTabs>" + "<signHereTabs>" + "<signHere>" + "<xPosition>" + signatureXPos + "</xPosition>" + "<yPosition>" + signatureYPos + "</yPosition>" + "<documentId>"+documentId+"</documentId>" + "<pageNumber>" + signaturePage + "</pageNumber>" + "</signHere>" + "</signHereTabs>" + "</tabs>" + "</signer>" + "</signers>" + "</recipients>" + "</envelopeDefinition>";
This is not a bug in DocuSign. This is the design of the captive recipient type. Captive recipients aren’t known users in DocuSign, therefore the title does not exist. The solution is very simple, you’d want to use a text tab and then prepopulate it’s value. + "<textTabs>" + "<text>" + "<xPosition>" + titleXPos + "</xPosition>" + "<yPosition>" + titleYPos + "</yPosition>" + "<documentId>"+documentId+"</documentId>" + "<pageNumber>" + titlePage + "</pageNumber>" + "<name>GM</name><width>200</width>" + "<value>General Manager</value>" + "<tabLabel>General Manager</tabLabel>" + "<required>false</required>" + "</text>" + "</textTabs>"