-1

I embedded the ontology and now I have the URI with the embedded number. So I could not find a solution. Here is my data on how it looks.

http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#MolecularCompositionPercentage -4.123444 -2.5054626 0.6315763
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#ActivatorMolCompPercentage -4.110525 -2.5071502 1.8669895
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#CaO_MolCompPerc -1.5034174 -1.2245587 0.004379553
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#TiO2_MolCompPerc -1.5249356 -1.2280324 0.014157391
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#Na2O_MolCompPerc -1.5057676 -1.2214838 0.008744555
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#Fe2O3_MolCompPerc -1.5426993 -1.2505695 -0.0022504963
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#K2O_MolCompPerc -1.5550603 -1.2507081 0.030926235
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#MgO_MolCompPerc -1.5091093 -1.2426957 -0.01673266
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#MnO_MolCompPerc -1.5519857 -1.2486249 0.005432705
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#Al2O3_MolCompPerc -1.582312 -1.2757595 -0.012372944
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#Cl_MolCompPerc -1.5798051 -1.2659851 0.0036723414
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#LossOnIgnition_MolCompPerc -1.5719161 -1.2368073 0.0034897649
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#SiO2_MolCompPerc -1.5483932 -1.2457571 0.025666924
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#SO3_MolCompPerc -1.5837562 -1.2600871 -0.009976057
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#P2O5_MolCompPerc -1.6092789 -1.2806703 -0.0026384927
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#WaterMolCompPercentage -4.8089037 -2.8734853 0.88688195
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#PowderStructure -1.4129046 -1.598844 3.1255267
http://www.semanticweb.org/btorres/ontologies/2021/7/AAC_Ontology#SiO2(mol-) -3.9019644 -2.430823 2.3417187

For example, in the first line I want to get only these numbers:

-4.123444 -2.5054626 0.6315763
my_list = []
with open('3D_10W_Hermit.txt' , newline='') as f:
    for line in f:
        my_list.append(line[-1])
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
TheAfg
  • 19
  • 1
  • 7

5 Answers5

1

You can use the split function to split your lines at the space character (default behavior of split()). This will return a list containing all the sub-units, then you can use Python's list slicing to get only the numbers.

Example:

my_list = []
with open('3D_10W_Hermit.txt' , newline='') as f:
    for line in f:
        my_list.append(line.split()[1:])

Output

['-4.123444 -2.5054626 0.6315763', '-4.110525 -2.5071502 1.8669895', ...]

Useful pages to understand more about the used techniques:
Official documentation about split()
Understanding slicing notation

Pitto
  • 8,229
  • 3
  • 42
  • 51
  • 1
    Thank you, your solution is also correct, but it gets only the last number, where I want all the three numbers. I posted the solution. – TheAfg Oct 14 '21 at 07:28
  • Please update your page, I edited it @TheAfg – Pitto Oct 14 '21 at 07:30
  • 2
    Why not `data.split()[1:])` – OneCricketeer Oct 14 '21 at 07:31
  • Good point @OneCricketeer! I updated my answer. About your suggested solution that would mean to keep all the numbers in a single variable, we don't know if this is what the user wants. – Pitto Oct 14 '21 at 07:31
1

you can extract it like this :

line_nums = str(line).split()[1:]

line_nums would contain the list of numbers you are looking for. You can modify this to suit your problem.

AbrarWali
  • 607
  • 6
  • 19
1

I think this may be what you're looking for:

my_list = []
with open('3D_10W_Hermit.txt') as o:
    for line in o:
        t = line.split()
        my_list.append(' '.join([t[-3],t[-2],t[-1]]))
print(my_list)

...or if the first token is guaranteed not to contain any whitespace then it's as simple as:

my_list = []
with open('3D_10W_Hermit.txt') as o:
    for line in o:
        my_list.append(' '.join(line.split()[1:]))
print(my_list)
0

Use split() with maxsplit optional parameter and get the second element in the result list.

my_list.append(line.split(" ",1)[1]) # this would extract any text after the first space. Those would be your numbers
LeelaPrasad
  • 416
  • 3
  • 4
-2

My approach was correct. Here I just change the index range.

my_list = []
with open('3D_10W_Hermit.txt' , newline='') as f:
    for line in f:
        my_list.append(line[-33:])
TheAfg
  • 19
  • 1
  • 7
  • 1
    This approach will not work in case your last number starts at a different index – Pitto Oct 14 '21 at 07:27
  • But if you look at the file, I have three indexes for all. I embedded in 3D. so there is no more elements. – TheAfg Oct 14 '21 at 07:30
  • 1
    No. We're saying that if the length of your numbers (including spaces and other characters) does not equal exactly 33 characters, then this would cause an issue – OneCricketeer Oct 14 '21 at 07:33