在python中将以空格分隔的文件转换为逗号分隔的值文件 [英] Convert a space delimited file to comma separated values file in python
问题描述
我对Python很陌生.我知道已经问过这个问题,对此我表示歉意,但是这种新情况的区别在于字符串之间的空格不相等.我有一个名为 coord 的文件,其中包含以下以空格分隔的字符串:
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
请注意第一列中每个字符串开头之前的空格.因此,我尝试了以下操作,以将其转换为csv:
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]) )
运行脚本时,在脚本的第一部分中会得到 coordv 的以下内容,这当然是非常错误的:
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
我在.replace中尝试了各种可能性,但均未成功,到目前为止,我还没有找到有关如何执行此操作的任何信息来源.从此 coord 文件获取逗号分隔值的最佳方法是什么?我感兴趣的是在python中使用csv模块选择第4:6列,最后使用numpy如下导入它们:
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.
推荐答案
将其替换为第一行. 它不是很漂亮,但是会为您提供csv格式.
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
如果您希望输出文件将所有内容放在不同的行上,则可以添加
outfile.write("\n")
在for循环的末尾,但是我不认为您后面的代码可以像这样使用它.
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.
这篇关于在python中将以空格分隔的文件转换为逗号分隔的值文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!