Moveton
Moveton

Reputation: 255

How to edit the *.txt or *.dat file information in Python?

I am a very beginner in Python and have the next 'problem'. I would be glad, if you could help me)

I have a *.dat file (let's name it file-1, first row is just a headline which I use only here to mark the columns) which looks like:

1   2   3       4   5   6

6   5   -1000   ""  ""  ""
6   5   -1000   ""  ""  ""

6   5   -1000   ""  ""  ""

6   5   -1000   ""  ""  ""
6   5   -1000   ""  ""  ""
6   5   -1000   ""  ""  ""
6   5   -1000   ""  ""  ""

I need it to be like (file-1 (converted)):

6   5   1   -1000
6   5   1   -1000
6   5   2   -1000
6   5   3   -1000
6   5   3   -1000
6   5   3   -1000
6   5   3   -1000

So, file-1 has 9 rows (7 with information and 2 empty) and 6 columns and I have to do the next:

  1. Delete the last 3 columns in the file-1.
  2. Add 1 new column that will take place between the columns 2 and 3.
  3. The value of this new column should be increased by 1 unit (like '+= 1') after passing the empty line.
  4. Delete all the empty lines. The result is represented as the 'file-1 (converted)'.

I've tried to do this but stucked. For now I am on the level of:

import sys
import csv

with open("file-1.dat", "r", newline="") as f:
    sys.stdout = open('%s2 (converted).txt' % f.name, 'a')
    incsv = csv.reader(f, delimiter="\t")
    for row in incsv:
        if len(row) == 6:
            i = 0
            row = row[0:3]
            row.insert(2, i)
            print(row)

and it looks like:

['6', '5', 0, '-1000']
['6', '5', 0, '-1000']

['6', '5', 0, '-1000']

['6', '5', 0, '-1000']
['6', '5', 0, '-1000']
['6', '5', 0, '-1000']
['6', '5', 0, '-1000']

I don't know for now how to change 0 to 1 and 2 and so on, so it could be like:

['6', '5', 0, '-1000']
['6', '5', 0, '-1000']

['6', '5', 1, '-1000']

['6', '5', 2, '-1000']
['6', '5', 2, '-1000']
['6', '5', 2, '-1000']
['6', '5', 2, '-1000']

And the result should be like the 'file-1 (converted)' file.

P.S. All the examples are simplified, real file has a lot of rows and I don't know where the empty lines appear.

P.P.S. Sorry for such a long post, hope, it makes sense. Ask, suggest - I would be really glad to see other opinions) Thank you.

Upvotes: 2

Views: 3799

Answers (3)

Tanmaya Meher
Tanmaya Meher

Reputation: 1476

This is bit different without using csv module. Hope this helps. :)

import sys

count = 0
with open("file-1.dat", "r") as f:
    sys.stdout = open('%s2 (converted).txt' % f.name, 'a')
    for line in f:
        converted_line = line.split()[:-3] #split each line and remove last 3 column
        if not converted_line: # if list/line is empty
            count += 1 #increase count but DO NOT PRINT/ WRITE TO FILE
        else:
            converted_line.insert(2,str(count)) # insert between 2nd and 3rd column
            print ('\t'.join(converted_line)) # join them and print them with tab delimiter

Upvotes: 1

max0r
max0r

Reputation: 361

You need to increment i on every empty line

import sys
import csv

with open("file-1.dat", "r") as f:
    sys.stdout = open('%s2 (converted).txt' % f.name, 'a')
    incsv = csv.reader(f, delimiter="\t")
    incsv.next() # ignore first line
    i = 0
    for row in incsv:
        if len(row) == 6:
            row = row[0:3]
            row.insert(2, i)
            print(row)
        elif len(row) == 0:
            i += 1

Also, I couldn't execute your code on my machine (with Python 2.7.6). I changed the code according to run with Python 2.x.

Edit: I see it runs with Python 3.x

Upvotes: 1

yurib
yurib

Reputation: 8147

seems like you're almost there, you're just inserting i=0 all the time instead of the count of empty rows, try something like:

with open("file-1.dat", "r", newline="") as f:
    sys.stdout = open('%s2 (converted).txt' % f.name, 'a')
    incsv = csv.reader(f, delimiter="\t")
    empties = 0 # init empty row counter
    for row in incsv:
        if len(row) == 6:
            row = row[0:3]
            row.insert(2, empties) # insert number of empty rows
            print(row)
        else:
            empties += 1 # if row is empty, increase counter

Upvotes: 3

Related Questions