Read certain part of a line/string - python

I'm trying to figure out how to read a certain part of a string using python, but I can't seem to figure it out, and nobody has the solution I'm looking for.
I have multiple lines formatted similarly to this:
1235:9875:0.1234
Its separated with colons, but the thing is that the length of the line varies, so only reading a certain amount of characters wont work.
Anyone have any idea how to do this? I really need to know this and I hope that this can help other people in the future.

Getting the values into array as strings:
test_str = "1235:9875:0.1234"
number_str_arr = test_str.split(":") # ['1235', '9875', '0.1234']
Saving them as floats instead of strings (maybe what you want?)
number_arr = [float(num) for num in number_str_arr] # [1235.0, 9875.0, 0.1234]
How to access certain values:
first_num = number_arr[0] # 1235.0
last_num = number_arr[-1] # 0.1234

Related

Is it possible to hard declare a variable in Python?

I am trying to use a variable inside a substructure. I guess the variable should be of integer data type, and I am trying to add a loop here but it my data type is list since it contains multiple integers.
INV_match_id = [['3749052'],['3749522']]
from statsbombpy import sb
for x in range(2):
match=INV_match_id[x]
match_db = sb.events(match_id=match)
print(match)
I have tried to extract the data one by one using another variable, but still it got declared as list. Whenever I give direct values to "match" it works. for eg: if I add a line match=12546 the substructure takes the value properly.
Next thing I want to try is hard declare "match" variable as integer. Any input is appreciated. I am pretty new to Python.
Edit: Adding this solution from #quamrana here.
"So, to answer your original question: Is it possible to hard declare a variable in Python?, the answer is No. Variables in python are just references to objects. Objects can be of whatever type they want to be."
You said: " I want to loop and take the numbers one by one."
Did you mean this:
for match in INV_match_id:
match_db = sb.events(match_id=match)
I don't know what you want to do with match_db
Update:
"that single number is also declared as a list. like this- ['125364']"
Well if match == ['125364'] then it depends on whether you want: "125364" or 125364. I assume the latter since you talk a lot about integers:
for match in INV_match_id:
match = int(match[0])
match_db = sb.events(match_id=match)
Next Update:
So you have: INV_match_id = ['3749052','3749522']
This means that the list is a list of strings, so the code changes to this:
for match in INV_match_id:
match_db = sb.events(match_id=int(match))
Your original code was making match into a list of the digits of each number. (eg match = [1,2,5,3,6,4])
Reversionary Update:
This time we have: INV_match_id = [['3749052'],['3749522']]
that just means going back to the second version of my code above:
for match in INV_match_id:
match = int(match[0])
match_db = sb.events(match_id=match)
It's as simple as:
from statsbombpy import sb
INV_match_id = [['3749052'],['3749522']]
for e in INV_match_id:
match_db = sb.events(match_id=e[0])
print(match_db)
You have a list of lists albeit that the sub-lists only contain one item.
match_id can be either a string or int

I'm not sure if I have correctly converted a string to an integer?

I apologize if this post isn't formatted correctly, I've tried to make sure that it is but this is my first time posting so I imagine I may have gotten something wrong.
I'm a complete beginner learning Python 3 and am going through a Udemy course where currently we are learning about joins and splits.
A challenge was set to convert a string of numbers to integers. The code I have written returns the values as individual numbers, however I am not sure whether these values were converted to integers or still are strings? The instructor of the course did not use the method that I have below and his method returned a list of integers.
I used the isdigit() method to see whether or not 'digits' returned True which it did. However I feel as though I've gone wrong somewhere?
I imagine this is an extremely basic question but any sort of clarity would be greatly appreciated!
numbers = "9,223,372,036,854,775,807"
for digits in numbers.split(","):
print(int(digits))
Here's an alternative to #Bryan Deng's answer:
num = '123,456,789'
num = num.replace(',', '') # replaces all commas with a '' empty string
# This way we don't have to convert to list than back to string
print(num)
# 123456789
Yes, you are outputting numbers.
If you want to output a list of numbers, you can achieve it in two ways.
create an empty list and push number into it.
numbers = "9,223,372,036,854,775,807"
ans = []
for digits in numbers.split(","):
ans.append(int(digits))
print(ans)
use list comprehension.
numbers = "9,223,372,036,854,775,807"
print([int(element) for element in numbers.split(",")])
I'm assuming you're asking to convert numbers from a string to an int using splits and joins. Here's one way to do it:
numbers = "123,456,789"
numbers.split(",") # numbers is now a list -> ["123", "456", "789"]
"".join(numbers) # converts numbers back into a string -> "123456789"
print(int(numbers))
# >>> 123456789

Converting String Data Values with two commas from csv or txt files into float in python

I just received a dataset from a HPLC run and the problem I ran into is that the txt data from the software generates two dotted separated values for instance "31.456.234 min". Since I want to plot the data with matplotlib and numpy I can only see the data where the values are not listed with two commas. This is due to every value which is smaller than 1 is represented with one comma like "0.765298" the rest of the values is, as aforementioned, listed with two commas.
I tried to solve this issue with a .split() and .find() method, however, this is rather inconvenient and I was wondering whether there would be a more elegant way to solve this issue, since I need in the end again x and y values for plotting.
Many thanks for any helping answers in advance.
This is not very clear regarding comma and dots.
For the decimal number you say that you have comma but you show a dot : 0.765298
I guess you can not have dots for either thousand separator and decimal...
If you have english notation I guess the numbers are:
"31,456,234 min" and "0.765298"
In this case you can use the replace method :
output = "31,456,234"
number = float(output.replace(',',''))
# result : 31456234.0
EDIT
Not very sure to have understood what you are looking for and the format of the numbers...
However if the second comma in 31.456.234 is unwanted here is a solution :
def conv(n):
i = n.find('.')
return float(n[:i]+'.'+n[i:].replace('.',''))
x = '31.456.234'
y = '0.765298'
print(conv(x)) # 31.456234
print(conv(y)) # 0.765298

Changing a part of string to the subscript

I have a list of strings, I want to place '_Octa', '_Tet' and so on in subscripts
original = ['VO₆_Octa', 'FeO₄_Tet', 'FeO₆_Oct', 'BaO₉_Tsf', 'PrO₆_Oct', 'CaO₆_Oct',
'HgO₂_Lin', 'CrO₆_Oct', 'AgO₄_Tet', 'EuO₉_Tsf']
What I want is posted in the screenshot
I have hundreds of such strings in a list. For numbers, I have found many such answers and I am able to apply in my case as well. Is there a better way to do it for such strings? Any help or pointers to similar problems would be great.
Use these!
SUB =
str.maketrans("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",
"₀₁₂₃₄₅₆₇₈₉ₐᵦ𝒸𝒹ₑfgₕᵢⱼₖₗₘₙₒₚqᵣₛₜᵤᵥwₓyz🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿₋")
SUP =
str.maketrans("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",
"⁰¹²³⁴⁵⁶⁷⁸⁹ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖᵠʳˢᵗᵘᵛʷˣʸᶻᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖᵠʳˢᵗᵘᵛʷˣʸᶻ‾")
Here's the code:
original = ['VO₆_Octa', 'FeO₄_Tet', 'FeO₆_Oct', 'BaO₉_Tsf', 'PrO₆_Oct', 'CaO₆_Oct',
'HgO₂_Lin', 'CrO₆_Oct', 'AgO₄_Tet', 'EuO₉_Tsf']
SUB =
str.maketrans("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",
"₀₁₂₃₄₅₆₇₈₉ₐᵦ𝒸𝒹ₑfgₕᵢⱼₖₗₘₙₒₚqᵣₛₜᵤᵥwₓyz🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿₋")
SUP =
str.maketrans("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",
"⁰¹²³⁴⁵⁶⁷⁸⁹ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖᵠʳˢᵗᵘᵛʷˣʸᶻᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖᵠʳˢᵗᵘᵛʷˣʸᶻ‾")
new = []
for item in original:
x = item.split('_')
new.append(x[0] + "₋" + x[1].translate(SUB))
print(new)
As you might have noticed, some letters don't actually convert properly to lowercase.
This is because the alphabets for subscript and superscript don't actually exist as a proper alphabet in Unicode.
I've used various online converters and could only get the conversions of the letters that you see above (ie: excluding lowercase b,c,d,f,g,q,w,y,z).
However in my opinion, the better way to do this would be to format the string in some markup language (HTML, Latex etc).
You'll have to use simple <sub></sub> and <sup></sup> tags in HTML.

Breaking 1 String into 2 Strings based on special characters using python

I am working with python and I am new to it. I am looking for a way to take a string and split it into two smaller strings. An example of the string is below
wholeString = '102..109'
And what I am trying to get is:
a = '102'
b = '109'
The information will always be separated by two periods like shown above, but the number of characters before and after can range anywhere from 1 - 10 characters in length. I am writing a loop that counts characters before and after the periods and then makes a slice based on those counts, but I was wondering if there was a more elegant way that someone knew about.
Thanks!
Try this:
a, b = wholeString.split('..')
It'll put each value into the corresponding variables.
Look at the string.split method.
split_up = [s.strip() for s in wholeString.split("..")]
This code will also strip off leading and trailing whitespace so you are just left with the values you are looking for. split_up will be a list of these values.

Categories

Resources