0

I am trying to read in a table from a .CSV file which should have 5 columns. But, some rows have corrupt data..making it more than 5 columns.

How do I reject those rows and continue reading further ?

*Using

temp = read_table(folder + r'\temp.txt, sep=r'\t')

Just gives an error and stops the program*

I am new to Python...please help Thanks

2
  • are you sure you want a raw string for the delimiter? can you post your read_table function? Commented Dec 10, 2012 at 2:40
  • my delimiter is tab....that's how tab is used as a delimiter. The file is corrupted in some places. So, instead of 5 columns, there are 15...i need to reject the rows with more than 5 columns Commented Dec 10, 2012 at 2:43

1 Answer 1

2

Look into using Python's csv module.

Without testing the damaged file it is difficult to say if this will do the trick however the csvreader reads a csv file's rows as a list of strings so you could potentially check if the list has 5 elements and proceed that way.

A code example:

out = []
with open('file.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimeter='    ')
    for row in reader:
        if len(row) == 5:
            out.append(row) 
Sign up to request clarification or add additional context in comments.

7 Comments

I'm not totally familiar with the .lvm format but it seems that it is very similar to csv so it would be worth trying my suggestion. Failing that if you can open the .lvm file in a text editor and paste some sample data here that might help me work it out.
13/11/2012 7:31:59 AM t1= 17.1600, c1=-0.00034, s= 0.0000, lat=37 52.9112 S, lon=147 58.2424 E, hms=203149, dmy=121112 13/11/2012 7:32:00 AM t1= 17.1598, c1=-0.00036, s= 0.0000 13/11/2012 7:32:01 AM t1= 17.1598, c1=-0.00034, s= 0.0000, lat=37 52.9111 S, lon=147 58.2423 E, hms=203151, dmy=121112 IT IS SPACE DELIMITED
What happens when you try my suggestion?
first, it is not a CSV file...when i use the read function...f = open(folder + r'/FLNTU_RAW_12-11-13_0731_000001.lvm','rb') ln = f.read().....the row(len) is always 1
Read my suggestion properly. It doesn't really matter if it is a csv file or not I would still try to use the csv module. Using open() to open the file doesn't work in the same way that using the csv module does.
|

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.