我是Python的新手。我知道这已经被问到了,我道歉,但这种新情况的不同之处在于字符串之间的空格不相等。我有一个名为 coord 的文件,其中包含以下以空格分隔的字符串:
1 C 6.00 0.000000000 1.342650315 0.000000000
2 C 6.00 0.000000000 -1.342650315 0.000000000
3 C 6.00 2.325538562 2.685300630 0.000000000
4 C 6.00 2.325538562 -2.685300630 0.000000000
5 C 6.00 4.651077125 1.342650315 0.000000000
6 C 6.00 4.651077125 -1.342650315 0.000000000
7 C 6.00 -2.325538562 2.685300630 0.000000000
8 C 6.00 -2.325538562 -2.685300630 0.000000000
9 C 6.00 -4.651077125 1.342650315 0.000000000
10 C 6.00 -4.651077125 -1.342650315 0.000000000
11 H 1.00 2.325538562 4.733763602 0.000000000
12 H 1.00 2.325538562 -4.733763602 0.000000000
13 H 1.00 -2.325538562 4.733763602 0.000000000
14 H 1.00 -2.325538562 -4.733763602 0.000000000
15 H 1.00 6.425098097 2.366881801 0.000000000
16 H 1.00 6.425098097 -2.366881801 0.000000000
17 H 1.00 -6.425098097 2.366881801 0.000000000
18 H 1.00 -6.425098097 -2.366881801 0.000000000
请注意第一列中每个字符串开头之前的空格。所以我尝试了以下顺序将其转换为csv:
with open('coord') as infile, open('coordv', 'w') as outfile:
outfile.write(infile.read().replace(" ", ", "))
# Unneeded columns are deleted from the csv
input = open('coordv', 'rb')
output = open('coordcsvout', 'wb')
writer = csv.writer(output)
for row in csv.reader(input):
if row:
writer.writerow(row)
input.close()
output.close()
with open("coordcsvout","rb") as source:
rdr= csv.reader( source )
with open("coordbarray","wb") as result:
wtr= csv.writer(result)
for r in rdr:
wtr.writerow( (r[5], r[6], r[7]) )
当我运行脚本时,我在脚本的第一部分中获得了 coordv 的以下内容,这当然是非常错误的:
, 1, C, , , 6.00, , 0.000000000, , 1.342650315, , 0.000000000
, 2, C, , , 6.00, , 0.000000000, -1.342650315, , 0.000000000
, 3, C, , , 6.00, , 2.325538562, , 2.685300630, , 0.000000000
, 4, C, , , 6.00, , 2.325538562, -2.685300630, , 0.000000000
, 5, C, , , 6.00, , 4.651077125, , 1.342650315, , 0.000000000
, 6, C, , , 6.00, , 4.651077125, -1.342650315, , 0.000000000
, 7, C, , , 6.00, -2.325538562, , 2.685300630, , 0.000000000
, 8, C, , , 6.00, -2.325538562, -2.685300630, , 0.000000000
, 9, C, , , 6.00, -4.651077125, , 1.342650315, , 0.000000000
, 10, C, , , 6.00, -4.651077125, -1.342650315, , 0.000000000
, 11, H, , , 1.00, , 2.325538562, , 4.733763602, , 0.000000000
, 12, H, , , 1.00, , 2.325538562, -4.733763602, , 0.000000000
, 13, H, , , 1.00, -2.325538562, , 4.733763602, , 0.000000000
, 14, H, , , 1.00, -2.325538562, -4.733763602, , 0.000000000
, 15, H, , , 1.00, , 6.425098097, , 2.366881801, , 0.000000000
, 16, H, , , 1.00, , 6.425098097, -2.366881801, , 0.000000000
, 17, H, , , 1.00, -6.425098097, , 2.366881801, , 0.000000000
, 18, H, , , 1.00, -6.425098097, -2.366881801, , 0.000000000
我在.replace尝试了不同的可能性,没有任何成功,到目前为止,我还没有找到任何关于如何做到这一点的信息来源。从这个 coord 文件中获取逗号分隔值的最佳方法是什么?我感兴趣的是在python中使用csv模块选择第4:6列,最后使用numpy导入它们,如下所示:
from numpy import genfromtxt
cocmatrix = genfromtxt('input', delimiter=',')
如果有人能帮我解决这个问题,我会很高兴的。
答案 0 :(得分:11)
您可以使用csv:
import csv
with open(ur_infile) as fin, open(ur_outfile, 'w') as fout:
o=csv.writer(fout)
for line in fin:
o.writerow(line.split())
答案 1 :(得分:5)
您可以使用python pandas,我已将您的数据写入data.csv
:
import pandas as pd
>>> df = pd.read_csv('data.csv',sep='\s+',header=None)
>>> df
0 1 2 3 4 5
0 1 C 6 0.000000 1.342650 0
1 2 C 6 0.000000 -1.342650 0
2 3 C 6 2.325539 2.685301 0
3 4 C 6 2.325539 -2.685301 0
4 5 C 6 4.651077 1.342650 0
5 6 C 6 4.651077 -1.342650 0
...
关于这一点的好处是访问你可以使用的基础numpy数组df.values
:
>>> type(df.values)
<type 'numpy.ndarray'>
使用逗号分隔符保存数据框:
>>> df.to_csv('data_out.csv',header=None)
Pandas是一个用于管理大量数据的绝佳库,作为奖励它可以很好地处理numpy。还有一个很好的机会,这比使用csv
模块要快得多。
答案 2 :(得分:3)
用这个替换你的第一位。 它不是超级漂亮,但它会给你一个csv格式。
with open('coord') as infile, open('coordv', 'w') as outfile:
for line in infile:
outfile.write(" ".join(line.split()).replace(' ', ','))
outfile.write(",") # trailing comma shouldn't matter
如果你想让outfile在你可以添加的不同行上拥有所有内容
在for循环结束时outfile.write("\n")
,但我不认为你之后的代码会像这样使用它。
答案 3 :(得分:1)
>>> a = 'cah 1 C 6.00 0.000000000 1.342650315 0.000000000'
=> a = 'cah 1 C 6.00 0.000000000 1.342650315 0.000000000'
>>> a.split()
=> ['cah', '1', 'C', '6.00', '0.000000000', '1.342650315', '0.000000000']
>>> ','.join(a.split())
=> 'cah,1,C,6.00,0.000000000,1.342650315,0.000000000'
>>> ['"' + x + '"' for x in a.split()]
=> ['"cah"', '"1"', '"C"', '"6.00"', '"0.000000000"', '"1.342650315"', '"0.000000000"']
>>> ','.join(['"' + x + '"' for x in a.split()]
=> '"cah","1","C","6.00","0.000000000","1.342650315","0.000000000"'
答案 4 :(得分:1)
仅将文件名填入所需内容
with open('filename') as infile, open('output', 'w') as outfile:
outfile.write(infile.read().replace(" ", ","))
with open('filename') as infile, open('output', 'w') as outfile:
outfile.write(infile.read().replace(",", " "))
答案 5 :(得分:0)
为什么不逐行读取文件?将一行拆分为一个列表,然后使用','重新加入一个列表。
答案 6 :(得分:0)
csv模块很好,或者没有这样做的方法:
#!/usr/local/cpython-3.3/bin/python
with open('input-file.csv', 'r') as infile, open('output.csv', 'w') as outfile:
for line in infile:
fields = line.split()
outfile.write('{}\n'.format(','.join(fields)))
答案 7 :(得分:0)
import csv
import os
for x in range(0,n): #n = max number of files
with open('input{}.txt'.format(x)) as fin, open('output.csv', 'a') as fout:
csv_output=csv.writer(fout)
for line in fin:
csv_output.writerow(line.split())