0

how to separate this line, I need just the numbers

PD-1358 393;

I would like to increment a variable X and a Y

 X   Y
PD-1358 393;

I am using the CSV command (csv_file, delimiter='-') but I can not separate these numbers. Can anybody help me?

import csv

with open('circulo.txt') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter='-')

    line_count = 0
    for row in csv_reader:

         print(f'\t {row[0]}')

For the moment I've only been able to separate this part

2
  • 2
    please post your code as well Commented Apr 19, 2019 at 1:06
  • 1
    It is not quite clear the expected output and input. Also as mentioned above, please post the code you tried. Commented Apr 19, 2019 at 1:11

3 Answers 3

2

Using regex you can simply do:

import re

re.findall(r'\d+', "PD-1358 393") 

Result:

['1358', '393']
Sign up to request clarification or add additional context in comments.

Comments

0

Don't know of an easy way to do this, but assuming you've turned the data into a string like so:

s = 'PD-1358 393'

Then create a generator to go to the first instance of a digit:

i = next((index for (index,value) in enumerate(s) if value.isdigit()))

And then split:

s[i:].split()

Returns:

['1358', '393']

Comments

0

If you want numbers in the final array:

line = 'PD-1358 393;'

parts = line.split(' ')

numbers = [int(''.join(i for i in part if i.isdigit())) for part in parts]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.