Convert a big table of value into string [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I have a table of value that i want to insert in my python code as a string :
I tried this but not working :
str(3.354219E-03 3.584506E-03 3.830603E-03 4.093597E-03 4.374646E-03
4.674992E-03 4.995957E-03 5.338959E-03 5.705510E-03 6.097227E-03
6.515837E-03 6.963188E-03 7.441252E-03 7.952137E-03 8.498098E-03
9.081543E-03 9.705044E-03 1.037135E-02 1.108341E-02 1.184435E-02
1.265753E-02 1.352655E-02 1.445522E-02 1.544766E-02 1.650823E-02
1.764162E-02 1.885282E-02 2.014718E-02 2.153040E-02 2.300859E-02
2.458826E-02 2.627639E-02 2.808042E-02 3.000831E-02 3.206855E-02
3.427025E-02 3.662310E-02 3.913749E-02 4.182451E-02 4.469601E-02
4.776465E-02 5.104398E-02 5.454845E-02 5.829352E-02 6.229571E-02
6.657268E-02 7.114329E-02 7.602769E-02 8.124744E-02 8.682555E-02)
Or another way would be to put quotation marks at the beggining and end of line but it's take too much time. If there are some options in Vim or Notepad i would be glad to here about it.

One way would of course to do it programmatically and immediately split your string so that you retrieve the desired table. If you read a multi-line string, you should use three quotation marks """.
If you are looking for a way to do it in a text editor, here are two solutions that work in VS Code (and many more editors)
Column-selection mode. Decently fast, because your inputs are all the same length. You can highlight all rows and then just add quotes accordingly.
Reg-ex replacement. You can use the following regex, see explanations here. Note that I highlighted the reg-ex mode (next to "1 of 200").
Regardless of the method, by using three quotation marks here as well, Python should be able to read your string including the "inner" quotation marks.

Related

Python check string pattern follows xxxx-yyyy-zzzz [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
The community is reviewing whether to reopen this question as of 4 months ago.
Improve this question
I am trying to build a check for strings that allows a pattern "xxxx-yyyy-zzzz". The string needs to have three blocks seperated by "-". Each block can contain "a-z", "A-Z", "_" (underscore) and "." (dot).
This is what I got to now:
file_name: str = "actors-zero-This_Is_Fine.exe.yml"
ab = re.compile("^([A-Z][0-9])-([A-Z][0-9])-([A-Z][0-9])+$")
if ab.match(file_name):
pass
else:
print(f"WARNING wrong syntax in {file_name}")
sys.exit()
Output is:
WARNING wrong name in actors-zero-This_Is_Fine.exe.yml
If I understand the question correctly, you want 3 groups of alphanumerical characters (plus .) separated by dashes:
Regex for this would be ^([\w.]+)-([\w.]+)-([\w.]+)$
^ matches the start of a string
Then we have 3 repeating groups:
([\w.]+) will match letters, numbers, underscores (\w) and dots (.) at least one time (+)
We make 3 of these, then separate each with a dash.
We finish off the regex with a $ to match the end of the string, making sure you're matching the whole thing.
What exactly is your question?
This looks alright so far. Your file name returns the warning because you have not specified the underscores in the third "block".
Instead of ([A-Z][0-9]). you could use character classes:
Like so: ^([\w.]+)-([\w.]+)-([\w.]+)$
Generally, I found the chapter in Automate The Boring Stuff on regex Pattern matching very concise and helpful:
https://automatetheboringstuff.com/2e/chapter7/
You will find the above table in this chapter also.

How do to get python to not calculate number variable with dash? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to pass a variable '3-123' to a method in python, but if I do str(3-123) I get '-120'. Tried iterating, but I got an error cause it's an int.
You simply pass the string "3-123".
Your expression str(3-123) tells Python to first evaluate what is in parentheses, which is very clearly the arithmetic expression 3-123. That evaluation mandates a subtraction.
UPDATE PER USER COMMENT
Since you just got it returned from REST, then it's already a string. It seems that your problem is that you're building an expression string to be evaluated in SQL. In this case, you need to build the string you're going to send to SQL, at the character level. For this one item you would extend your 3-123 string with quotation marks:
from_rest = "3-123" # In practice, this comes directly from your REST return value.
to_sql = '"' + from_rest + '"'
This leaves you with a variable that contains the string "3-123" -- seven characters, rather than the original five.
Is that what you needed?

Excluding/including strings in one re.compile statement in python to extract urls of interest [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So I'm trying to get urls that contain specific strings, but also avoiding urls that contain a bad string.
So I don't want any urls that contain the string "/inventory/all/", and I only want urls that contain either the string "/inventories/" or "/inventory/2017/"
So I've managed to exclude at least the urls with strings that contain "/inventory/all/" by:
get_urls = soup.findAll('a', href=re.compile('^(?!.*/inventory/all/).*$'))
But when I try to include the strings I do want to get, then it no longer works, I tried:
get_urls = soup.findAll('a', href=re.compile('^(?!.*/inventory/all/).*$'|/inventories/|/inventory/2017/'))
Thanks for the help, I'm quite the novice
you can use the following regex:
^(?=.*inventor(?:ies|y/2017))^(?:(?!inventory/all).)+$
^(?=.*inventor(?:ies|y/2017)) This is a look ahead that ensures that we are just looking for strings with either inventories or inventory/2017. For fewer backtracking, you need to anchor it ie ^ which shows that the matching should start at the beginning of the sentence. Thus just doing ^.*inventor(?:ies|y/2017).*$ should be enough since the only ones selected are the two.
^(?:(?!inventory/all).)+$ this part is a negative look ahead which asserts that from the beginning of the string to the end of the string there is no inverntory/all. I added this part in case you find a string that is of the format inventoy/2017/inventory/all This will be dropped.

remove hex value from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do I remove the hex value from a string in Python 2.7? Here is the string,
\xffDSI\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x01\x00\x08\x00\x00\x00\x00\x00\x00\x00#\x00\x00\x00#\x00\x00\x00\x01\x04\xb3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01\x00\x04\x00\x02\x00\x01\x00\x01\x00\x02\x00\x06\x00\x06"\x00\x00\x00\x00c\x01,\x00\x00\x06&\x00\x00\x00\x01\x01,\x00\x00\x06\'\x00\x00\x11\x98\x00\x19\x00\x00\x00(\x00\x00\x00\x00\x00\x0011_w\x00\x00\x00\x00\x00\x006\x00A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01,\x00\x00\x17\xbf\x00\x00\x11\x98\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
#b4
,sudd5ly1
!
toddl]
0
able
to
use
a
comput]
to
play
games4
\x00$\x00\x00\x018\x00\x00\x00\x00\x00\x01\x00\x19\x00\x02\x00\x03\x00\x04\x00\x05\x00\x1d\x00\x1f\x00\x06\x00\x07\x00\x08\x00\t\x00'],
['\x00\x0b\x00\x0c\x00\r\x00\x1b\x00\x0e\x00\x0f\x00\x10\x00\x13\x00\x11\x00\x14\x00\x1c\x00\x18\x00
I want to display only #b4 to games4. All the hex values should be removed. Thank you.
What I am trying to do is to read in the file type *.dxb, which display braille font. I was able to read the file but the output showed me all those \xffDSI\x00... and then #b4 ,sudd5ly1
The #b4 ,sudd5ly1 is only the part that I want the output to show so that I can do a comparison with other file.
Thank you again.
You can use something like this:
import string
s = '\xffDSI....'
cleaned = ''.join(c for c in s if c in string.printable)
This uses printable as the definition of "not a hex value", though it does include \x0b and \x0c (both printable whitespace characters).

python: convert string to regular expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am using python to do some text comparison. The text format is like 44=100. Let's say, I have 2 text, 44=100 and 44=3001. I call the string on the left of = is tag, right is value. Now I need to compare the tag and value for them. The tag must be the same, 44 equals 44, but the values don't have to, as long as its format is the same. ie. 100 and 3001 are in the same format(normal digits). But 1.0E+7 in 44=1.0E+7 is different. tThe point is on value comparison. ie. I write a script comp.py, when I run comp.py 2000 30010, I will get output true; while I run comp.py 100000 1.0E+8, output is false. How can I do it? I am thinking about converting the value into an regular expression and comparing it with other.
pseudo code:
rex1 = '100000'.getRegrex(), rex2 = '1.0E+8'.getRegrex(), rex1.compare(rex2)
Is it a feasible way? any advice?
rex1 = '100000'.getRegrex(), rex2 = '1.0E+8'.getRegrex(), rex1.compare(rex2)
Your approach is wrong. It is not only difficult but also illogical to "deduce" a regexp from a given string. What you would do is:
Define your types. With each type you would have a corresponding regexp.
Compare your input text against all your defined types and check which type it is of.
Compare the two types.
Actually, your idea of rex1 = '100000'.getRegrex() could be done
rex1 = re.compile('10000')
But as Thustmaster pointed out, you may want to define the regular expression with more abstraction of the pattern of your data.

Categories