0

There is a list of elements in python in the following form:

['96\t00:00:00:19\t-16,894725\t15,201475\t26,976\t52,471\t10,081275\t42,177475\t32,0962']
['97\t00:00:00:19\t-17,007775\t15,198775\t26,979\t52,471\t9,971225\t42,177775\t32,20655']
['98\t00:00:00:19\t-17,010475\t15,204175\t26,977\t52,471\t9,966525\t42,181175\t32,21465']
['99\t00:00:00:19\t-16,9997\t15,1907\t26,981\t52,473\t9,9813\t42,1717\t32,1904']
['100\t00:00:00:20\t-17,0024\t15,17995\t26,979\t52,476\t9,9766\t42,15895\t32,18235']

UPDATED:

I am using the following code:

import os
import csv

dirlisting=os.listdir(os.getcwd())

for fname in dirlisting:
if fname.endswith(".csv"):
    print "Processing: " + fname
    fi=open(fname, 'rb')
    data=fi.read()
    fi.close()
    fo=open(fname, 'wb')
    fo.write(data.replace('\x00', ''))
    fo.close()

    with open(fname, 'rb') as csvfile:
        sreader=csv.reader(csvfile, delimiter=' ')             
        rows=[r for r in sreader]
        for row in range(6, len(rows)):
            print rows[row]

I need to split this list elements or to convert them in some other form so that they can be further processed.

2
  • 3
    To what form do you want to convert them? Also, what you’ve written here is five separate lists… did you mean to write that the five strings are in the same list together? Commented Feb 15, 2013 at 23:05
  • Is this the actual format of your data? What you have in the codeblock of your question is not a list of string elements, but a syntactically invalid series of single element lists each containing a string. Commented Feb 15, 2013 at 23:09

3 Answers 3

2

On what delimiter? Have you seen the split() method available on all strings?

Sign up to request clarification or add additional context in comments.

4 Comments

delimiter is \t. Yes, I have tried split but can't make use of it. It is always giving an error that list object has no attribute 'split'
...yeah, because you split the string, not the list. Where are you getting this data from? A bunch of single element lists containing strings is somewhat of a strange format.
Its a csv file with spaces as deimiters.
@sr2222 ['somethingsomething'] is a single element list. 'somethingsomething' would be a string. You need to use listname[0].split() with listname = ['somethingsomething']
1

If you are using \t as delimeter, you should just change the line

sreader = csv.reader(csvfile, delimiter=' ')

to

sreader = csv.reader(csvfile, delimiter='\t')

No postprocessing needed.

Comments

0

I’ll assume that what you have is a list of strings, not the series of single-element lists that you put in the question. You can do something like this:

strings = ['96\t00:00:00:19\t-16,894725\t15,201475\t26,976\t52,471\t10,081275\t42,177475\t32,0962',
  '97\t00:00:00:19\t-17,007775\t15,198775\t26,979\t52,471\t9,971225\t42,177775\t32,20655',
  '98\t00:00:00:19\t-17,010475\t15,204175\t26,977\t52,471\t9,966525\t42,181175\t32,21465',
  '99\t00:00:00:19\t-16,9997\t15,1907\t26,981\t52,473\t9,9813\t42,1717\t32,1904',
  '100\t00:00:00:20\t-17,0024\t15,17995\t26,979\t52,476\t9,9766\t42,15895\t32,18235']
split_strings = [string.split('\t') for string in strings]

This will yield a list of lists:

[['96', '00:00:00:19', '-16,894725', '15,201475', '26,976', '52,471', '10,081275', '42,177475', '32,0962'],
 ['97', '00:00:00:19', '-17,007775', '15,198775', '26,979', '52,471', '9,971225', '42,177775', '32,20655'],
 ['98', '00:00:00:19', '-17,010475', '15,204175', '26,977', '52,471', '9,966525', '42,181175', '32,21465'],
 ['99', '00:00:00:19', '-16,9997', '15,1907', '26,981', '52,473', '9,9813', '42,1717', '32,1904'],
 ['100', '00:00:00:20', '-17,0024', '15,17995', '26,979', '52,476', '9,9766', '42,15895', '32,18235']]

I hope this helps you get started.

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.