0

So I have a text file consisting of one column, each column consist two numbers

190..255
337..2799
2801..3733
3734..5020
5234..5530
5683..6459
8238..9191
9306..9893

I would like to discard the very 1st and the very last number, in this case, 190 and 9893. and basically moves the rest of the numbers one spot forward. like this

My desired output

255..337
2799..2801
3733..3734
5020..5234
5530..5683
6459..8238
9191..9306

I hope that makes sense I'm not sure how to approach this

0

6 Answers 6

4
lines = """190..255
337..2799
2801..3733"""

values = [int(v) for line in lines.split() for v in line.split('..')]
# values = [190, 255, 337, 2799, 2801, 3733]

pairs = zip(values[1:-1:2], values[2:-1:2])
# pairs = [(255, 337), (2799, 2801)]

out = '\n'.join('%d..%d' % pair for pair in pairs)
# out = "255..337\n2799..2801"
Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

with open(filename, 'r') as f:
    lines = f.readlines()

numbers = []
for row in lines:
    numbers.extend(row.split('..'))

numbers = numbers[1:len(numbers)-1]
newLines = ['..'.join(numbers[idx:idx+2]) for idx in xrange(0, len(numbers), 2]

with open(filename, 'w') as f:
    for line in newLines:
        f.write(line)
        f.write('\n')

Comments

3

Try this:

  • Read all of them into one list, split each line into two numbers, so you have one list of all your numbers.
  • Remove the first and last item from your list
  • Write out your list, two items at a time, with dots in between them.

Here's an example:

a = """190..255
       337..2799
       2801..3733
       3734..5020
       5234..5530
       5683..6459
       8238..9191
       9306..9893"""
a_list = a.replace('..','\n').split()
b_list = a_list[1:-1]
b = ''
for i in range(len(a_list)/2):
    b += '..'.join(b_list[2*i:2*i+2]) + '\n'

Comments

3
temp = []
with open('temp.txt') as ofile:
    for x in ofile:
        temp.append(x.rstrip("\n"))
for x in range(0, len(temp) - 1):
    print temp[x].split("..")[1] +".."+ temp[x+1].split("..")[0]
    x += 1

Comments

2

Maybe this will help:

def makeColumns(listOfNumbers):
    n = int()
    while n < len(listOfNumbers):
        print(listOfNumbers[n], '..', listOfNumbers[(n+1)])
        n += 2

def trim(listOfNumbers):
    listOfNumbers.pop(0)
    listOfNumbers.pop((len(listOfNumbers) - 1))

listOfNumbers = [190, 255, 337, 2799, 2801, 3733, 3734, 5020, 5234, 5530, 5683, 6459, 8238, 9191, 9306, 9893]

makeColumns(listOfNumbers)
print()

trim(listOfNumbers)
makeColumns(listOfNumbers)

Comments

0

I think this might be useful too. I am reading data from a file name list.

data = open("list","r")
temp = []
value = []
print data
for line in data:
    temp = line.split("..")
    value.append(temp[0])
    value.append(temp[1])

for i in range(1,(len(value)-1),2):
    print value[i].strip()+".."+value[i+1]

print value

After reading the data I split and store it in the temporary list.After that, I copy data to the main list value which have all of the data.Then I iterate from the second element to second last element to get the output of interest. strip function is used in order to remove the '\n' character from the value.

You can later write these values to a file Instead of printing out.

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.