python输出结果空格分割,在python中将以空格分隔的文件转换为逗号分隔的值文件...

I am very new to Python. I know that this has already been asked, and I apologise, but the difference in this new situation is that spaces between strings are not equal. I have a file, named coord, that contains the following space delimited strings:

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

Please, note the spaces before the start of each string in the first column. So I have tried the following in order of converting it to 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]) )

When I run the script, I get the following for the coordv in the very first part of the script, which is of course very wrong:

, 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

I have tried different possibilities in .replace without any success, and so far I haven't found any source of information on how I could do this. What would be the best way to get a comma-separated values from this coord file? What I'm interested is in using then the csv module in python to choose columns 4:6 and finally use numpy to import them as follows:

from numpy import genfromtxt

cocmatrix = genfromtxt('input', delimiter=',')

I'd be very glad if somebody could help me with this problem.

解决方案

replace your first bit with this.

it's not super pretty but it will give you a csv format.

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

if you want the outfile to have everything on different lines you could add

outfile.write("\n") at the end of the for loop, but i dont think your code that follows this will work with it like that.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值