1

你好我这里有个小问题。

我有一个带有数字的文本文件,看起来像这样

2.131583
2.058964
6.866568
0.996470
6.424396
0.996004
6.421990

fList = [s.strip() for s in open('out.txt').readlines()]
outStr = ''
for i in fList:
      outStr += (i+',')
f = open('text_to_csv.csv', 'w')
f.write(outStr.strip())
f.close()

我能够生成一个 CSV,所有数据都存储在其中,但都在一行中。我想把它们分成两列。

是否有任何简单的添加可以使 CSV 看起来像这样?

2.131583 2.058964
6.866568 0.996470
6.424396 0.996004
4

6 回答 6

2

更好的方法是使用 csv 模块。你可以这样写

import csv

with open('text_to_csv.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter=',',quoting=csv.QUOTE_MINIMAL)
    for i in range(0, len(fList), 2):
        writer.writerow(fList[i:i+2])
于 2013-10-30T17:32:09.717 回答
1
fList = [s.strip() for s in open('out.txt').readlines()]
outStr = ''
count = 0
for i in fList:
      outStr += (i+',')
      if count % 2 == 0: # You can replace 2 with what ever number you of columns you need
          outStr += ('\r\n') # Make the return correct for your system
      count += 1
f = open('text_to_csv.csv', 'w')
f.write(outStr.strip())
f.close()
于 2013-10-30T17:13:20.260 回答
0

像这样的东西:

with open('out.txt', 'r') as fList, open('text_to_csv.csv', 'w') as f:
    i = 0
    for line in fList:
        f.write(line)
        f.write('\n' if i% 2 == 0 else '\t')`
于 2013-10-30T17:17:03.313 回答
0

我不太确定你的意思,但我认为你的预期输出是:

2.131583,2.058964,
6.866568,0.996470,
6.424396,0.996004,
6.421990

我的代码:

with open('out.txt', 'r') as fif, open('text_to_csv.csv', 'w') as fof:
    fList = ','.join([v.strip() if i % 2 else '\n'+v.strip()
                      for i, v in enumerate(fif.readlines())])[1:]
    fof.write(fList)


有趣的点:

如果你想去掉文件末尾的“,”,只需通过join()函数连接列表。

flat_string = ','.join([item1,...,])

对于列表中奇数项的前导换行符,我已经列举了它。

index, value enumerate([item1,...,])

并通过 modulo-operator 找到奇数项index % 2

使用“inline-if”,您可以即时检查。

至少我排除了字符串开头的多余换行符[1:]

于 2013-10-30T17:50:07.667 回答
0

如果您对将原始文件中的条目存储在新列表中不感兴趣,而只想要输出文件,您也可以执行以下操作:

fList = [s.strip() for s in open('out.txt').readlines()]
f = open('text_to_csv.csv', 'w')
for i in range(0,len(fList)-1,2):
    f.write(fList[i] + "," + fList[i+1] + "\n")

f.close()
于 2013-10-30T17:26:50.933 回答
0

如果您在内存中有一个列表(来自读取文件),只需将列表重新格式化为您想要的:

input='''\
2.131583
2.058964
6.866568
0.996470
6.424396
0.996004
6.421990'''    

cols=2

data=input.split()       # proxy for a file
print data
print '==='
for li in [data[i:i+cols] for i in range(0,len(data),cols)]:
    print li

印刷:

['2.131583', '2.058964', '6.866568', '0.996470', '6.424396', '0.996004', '6.421990']
===
['2.131583', '2.058964']
['6.866568', '0.996470']
['6.424396', '0.996004']
['6.421990']

或者,使用一次 N 次文件读取习惯用法:

import itertools
cols=2
with open('/tmp/nums.txt') as fin:
    for li in itertools.izip_longest(*[fin]*cols):
        print li  
# prints
('2.131583\n', '2.058964\n')
('6.866568\n', '0.996470\n')
('6.424396\n', '0.996004\n')
('6.421990', None)

如果您想要一种文件过滤器,您可以将其组合成一个迭代器,一个迭代器输出:

import itertools
cols=2
with open('/tmp/nums.txt') as fin, open('/tmp/nout.txt','w') as fout:
    for li in itertools.izip_longest(*[fin]*cols):
        fout.write('\t'.join(e.strip() for e in li if e)+'\n')

输出文件现在将是:

2.131583    2.058964
6.866568    0.996470
6.424396    0.996004
6.421990

如果您只想编写完整的数字集的输出,即文件末尾小于cols总长度的剩余数字:

import itertools
cols=2
# last number '6.421990' not included since izip is used instead of izip_longest
with open('/tmp/nums.txt') as fin, open('/tmp/nout.txt','w') as fout:
    for li in itertools.izip(*[fin]*cols):
        fout.write('\t'.join(e.strip() for e in li)+'\n') 

那么输出文件是:

2.131583    2.058964
6.866568    0.996470
6.424396    0.996004
于 2013-10-30T17:15:45.367 回答