0

python中有一个元素列表,格式如下:

['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']

更新:

我正在使用以下代码:

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]

我需要拆分此列表元素或将它们转换为其他形式,以便进一步处理它们。

4

3 回答 3

2

在什么分隔符上?你见过split()所有字符串都可用的方法吗?

于 2013-02-15T23:03:11.247 回答
1

如果您\t用作分隔符,则应更改该行

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

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

无需后处理。

于 2013-02-15T23:22:40.100 回答
0

我假设您拥有的是一个字符串列表,而不是您在问题中提出的一系列单元素列表。你可以这样做:

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]

这将产生一个列表列表:

[['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']]

我希望这可以帮助您入门。

于 2013-02-15T23:15:58.467 回答