Need some help parsing a string in C - c

Using fgets, I have read in a line from a text file. The line may be something like this:
# O^6+ + H -> O^5+ + H^+
Or it may be this:
# Mg^12+ + H -> Mg^11+ + H^+
or this:
# Ne^10+ + He -> Ne^9+ + He^+
Or a multitude of other possibilities.
I am trying to extract the ion, the charge and the atom terms from the string.
I tried something like this:
sscanf(line,"# %2s^%d+ + %2s",cs->ION,&(cs->Z),cs->ATOM);
I also tried this:
sscanf(line,"# %[^^]s^%d+ + %2s",cs->ION,&(cs->Z),cs->ATOM); Because I was picking up the '^' character.
I just can't seem to get this to work for every case. Any suggestions are appreciated.

Your try with the format string
"# %[^^]s^%d+ + %2s"
was almost right, except that after the %[^^] there has to be no s, i. e.
"# %[^^]^%d+ + %2s"
works.

Related

How can I loop through a concatenation

I'm trying to split a word with a '.' after every letter which I was successful in doing, however, my next step is to split the current splitted words again but I dont want to repeat variations.
my expected output is this:
input word: amaxa
first loop will give - a.maxa, am.axa, ama.xa, amax.a
then the next split should give - a.m.axa, a.ma.xa,a.max.a,
Essentially I wanted different variations of the word with '.' being added when a full loop had been exhausted however, my main issue was I had '.'s appearing next to each other and I tried to use a continue statement but it didn't work. Below is my source code
print("enter email without #gmail.com")
word = input("word: ")
lenofword = len(word) - 1
for i in range(0,lenofword):
sliceword = word[:1+i] + "." + word[1+i:]
lis.append(sliceword)
print(sliceword)
for j in range(0,lenofword):
slices = sliceword[:1+j] + "." + sliceword[j+1:]
if slices[i:] == slices[:]:
continue
print(slices)
ouput given:
enter email without #gmail.com
word: amax
a.max
am.ax
a.m.ax
am..ax
am..ax
ama.x
a.ma.x
am.a.x
ama..x
basically i want to get rid of the 'am..ax' and 'ama..x'
It is easier to choose the two locations where the dots should be placed (may be the same location for the single dot case). For example:
for i in range(1, lenofword + 1):
for j in range(i, lenofword + 1):
sliced = ".".join(filter(None, [word[:i], word[i:j], word[j:]]))
print(sliced)
This will print the following for the input word "amax":
a.max
a.m.ax
a.ma.x
am.ax
am.a.x
ama.x

Iterating in dataframe and insert on text

There is a DataFrame loaded in pandas with size m*n, m can be big compare with columnes n which are values from 2 to 20.
each value from m*n has to be add to expecific text, it means that text between any value is constant.
I tried with For and If nested sentences, no good result how to make step from df.iloc[0,0] to df.iloc[m,n] and insert in text.
textA + df.iloc[0,0] + textB + df.iloc[0,1] + .... + textX + df.iloc[0,n]
textA + df.iloc[1,0] + textB + df.iloc[1,1] + .... + textX + df.iloc[1,n]
.
.
.
textA + df.iloc[m,0] + textB + df.iloc[m,1] + .... + textX + df.iloc[m,n]
I have 2 files, one include textA textB ... textX Second file is csv type where pandas dataframe is generated.
With dataframe and text prepare array above.
thanks for any tip.
Not exactly clear on your requirement but takae a look if the following code is able to help you iterate between df.iloc[0,0] to df.iloc[m,n] while adding information from your text file (in this case a text variable)
# i am using a dummy variable for the text data as i am not sure how your data look like
text = ['textA', 'textB', 'textC']
new_text = []
# using a nested for loop to iterate
for row in range(len(df)):
for col in range(len(df.columns)):
new_text.append(text[col] + str(df.iloc[row, col]))

Multiple line strings in Apache Zeppelin

I have a very long string that must be broken into multiple lines. How can I do that in zeppelin?
The error is error: missing argument list for method + in class String:
Here is the more complete error message:
<console>:14: error: missing argument list for method + in class String
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `$plus _` or `$plus(_)` instead of `$plus`.
val q = "select count(distinct productId),count(distinct date),count(distinct instock_inStockPercent), count(distinct instock_totalOnHand)," +
In Scala (using Apache Zeppelin as well as otherwise), you can write expressions covering multiple lines by wrapping them in parentheses:
val text = ("line 1"
+ "line 2")
Using parentheses
As Theus mentioned. One way is parentheses.
val text = ("line 1" +
"line 2")
Actually all multiline statements which break by semantics can be included by parentheses. like.
(object.function1()
.function2())
Using """
For multiline string. We can use """, like this,
val s = """line 1
line2
line3"""
The leading space before line2 and line3 will be included. If we don't want to to have the leading spaces. We can use like this.
val s = """line 1
|line2
|line3""".stripMargin
Or using different strip character
val s = """line 1
$line2
$line3""".stripMargin('$')

Can't display unicode characters from file properly

I'm writing a script which should operate on words from a number of files which have unicode characters in a form of something\u0142somethingelse.
I use python 3 so I suppose after reading line \u0142 should be replaced by 'ł' character, but it isn't. I receive "something\u0142somethingelse" in console.
After manually copying "bad" output from console and pasting it to: print("something\u0142somethingelse") it is displayed correctly.
Problematic part of the script:
list_of_files = ['test/stack.txt']
for file in list_of_files:
with open(file,'r') as fp:
for line in fp:
print(line)
print("something\u0142somethingelse")
stack.txt:
something\u0142somethingelse
Output:
something\u0142somethingelse
somethingłsomethingelse
I experimented with utf-8 encoding when opening this file and really I'm out of ideas...
I think you can do what you want with ast.literal_eval. This uses the same syntax as the Python interpreter to understand literals: like eval but safer. So this works, for example:
a = 'something\\u0142somethingelse'
import ast
b = ast.literal_eval('"' + a + '"')
print '"' + a + '"'
print b
The output should be:
"something\u0142somethingelse"
somethingłsomethingelse

even two string are same but when compare result are coming false

I am comparing two string.I am reading String 1 i.e expectedResult from excelsheet and String 2 i.e actualResult i am getting from web page by using " getElementByXPath("errorMsg_userPass").getText();
but when i equate two string even though they are same result of comparison are coming false i.e they are not same.
enter image description here
I don't know why it is happening like this .Please Help
use trim() to remove leading and trailing spaces!!
I recommend you looking at the exact bytes of the actual and expected strings. There might be for instance an unbreakable space instead of a regular space and then they will look the same but won't be the same for equals.
You can see the difference by running the following snippet:
String a = new String("a\u00A0b");
String b = new String ("a b");
System.out.println(a + "|" + Arrays.toString(a.getBytes()));
System.out.println(b + "|" + Arrays.toString(b.getBytes()));
Which will output:
a b|[97, -62, -96, 98]
a b|[97, 32, 98]

Resources