Python CSV读取和写;删除并替换PLUS:行尾为JSON格式 [英] Python CSV read-> write; remove and replace PLUS: end of line is JSON format
问题描述
我在让Python脚本执行我想做的事情时遇到问题.它似乎没有在修改我的文件.
I am having problems getting my Python script to do what I want. It does not appear to be modifying my file.
我要:
- 读取具有以下格式的* .csv文件PropertyName :: PropertyValue,…,PropertyName :: PropertyValue,{ExtPropertyName :: ExtPropertyValue},…,{ExtPropertyName :: ExtPropertyValue}
- 我要删除PropertyName ::并只保留PropertyValue的一列
- 我要添加标题行
我试图逐步用逗号替换::值,但似乎无法正常工作:
I was trying to step through replacing the :: values with a comma, but cant seem to get this to work:
fin = csv.reader(open('infile', 'rb'), delimiter=',')
fout = open('outfile', 'w')
for row in fin:
fout.write(','.join(','.join(item.split()) for item in row) + '::')
fout.close()
任何建议,无论是关于我的第一步问题,还是对于更大的图片分辨率,都总是受到欢迎.谢谢.
Any advice, whether on my first step problem, or to a bigger picture resolution is always appreciated. Thanks.
一个好人要我进行更新/编辑的请求!
UPDATE/EDIT asked for by a person nice enough to review for me!
这是* .csv文件(输入)的第一行
Here is the first line of the *.csv file (INPUT)
InnerDiameterOrWidth::0.1,InnerHeight::0.1,Length2dCenterToCenter::44.6743867864386,Length3dCenterToCenter::44.6768028159989,Length2dToInsideEdge::44.2678260053526,Length3dToInsideEdge::44.2717800813466,Length2dToOutsideEdge::44.6743867864386,Length3dToOutsideEdge::44.6768028159989,MinimumCover::0,MaximumCover::0,StartConnection::ImmxGisUtilityNetworkCommon.Connection,
在理想世界中,我希望文本文件看起来像(输出)
In a perfect world here is what I would like my text file to look like (OUTPUT)
InnerDiameterOrWidth, InnerHeight, Length2dCenterToCenter,,,,,,,,,,,
0.1,0.1,44.6743867864386
所以一个标题行和列中的值
so one header line and the values in column
已更新 JSON信息
每行的末尾都有JSON格式的文本:
The end of each line has JSON formatted text:
{StartPoint::7858.35924983374[%2C]1703.69341358077[%2C]-3.075},{EndPoint::7822.85045874375[%2C]1730.80294308742[%2C]-3.53962362760298}
我需要用标题将其拆分为X Y Z和X Y Z
WHich I need to split into X Y Z and X Y Z with headers
推荐答案
也许是这样的(假设每行具有相同的键,并且顺序相同):
Maybe something like this (assuming that each line has the same keys, and in the same order):
import csv
with open("diam.csv", "rb") as fin, open("diam_out.csv", "wb") as fout:
reader = csv.reader(fin)
writer = csv.writer(fout)
for i, line in enumerate(reader):
split = [item.split("::") for item in line if item.strip()]
if not split: # blank line
continue
keys, vals = zip(*split)
if i == 0:
# first line: write header
writer.writerow(keys)
writer.writerow(vals)
产生
localhost-2:coding $ cat diam_out.csv
InnerDiameterOrWidth,InnerHeight,Length2dCenterToCenter,Length3dCenterToCenter,Length2dToInsideEdge,Length3dToInsideEdge,Length2dToOutsideEdge,Length3dToOutsideEdge,MinimumCover,MaximumCover,StartConnection
0.1,0.1,44.6743867864386,44.6768028159989,44.2678260053526,44.2717800813466,44.6743867864386,44.6768028159989,0,0,ImmxGisUtilityNetworkCommon.Connection
我认为大多数代码都应该有意义,除了 zip(* split)
技巧外:基本上可以转置序列,即
I think most of that code should make sense, except maybe the zip(*split)
trick: that basically transposes a sequence, i.e.
>>> s = [['a','1'],['b','2']]
>>> zip(*s)
[('a', 'b'), ('1', '2')]
以使元素现在按其索引分组在一起(第一个元素在一起,第二个元素等等)
so that the elements are now grouped together by their index (the first ones are all together, the second, etc.)
这篇关于Python CSV读取和写;删除并替换PLUS:行尾为JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!