我有一个这样布局的文本文件,其中每个字段都是一个新行:
id = 606149
Category Name = Structural Columns
Family Name = Concrete-Square-Column
Type Name = EXIST RH C1 16 x 16
Document = 15050 Peavy Struct v2016_detached
Attachment Justification At Top = Minimum Intersection
Image = <None>
Offset From Attachment At Top = 0
id = 606151
Category Name = Structural Columns
Family Name = Concrete-Square-Column
Type Name = EXIST RH C2 16 x 16
Document = 15050 Peavy Struct v2016_detached
Attachment Justification At Top = Minimum Intersection
Image = <None>
Offset From Attachment At Top = 0
在我的代码中,我打开文本文件进行读取并打印出前三行进行测试。当我尝试在行尾添加逗号时,我在下面的行中得到了逗号:
def main():
count = 0
filename = "test.txt"
file = open(filename, "r")
for line in file:
if count == 3:
break
count = count + 1
line += ','
print line
通过这段代码我得到结果:
id = 606149
,
Category Name = Structural Columns
,
Family Name = Concrete-Square-Column
,
当我在连接逗号之前添加一条线以去除新行时:
line = line.strip('\n')"
我得到这个结果:
,id = 606149
,ategory Name = Structural Columns
,amily Name = Concrete-Square-Column
我无法将此文件解析为 CSV 格式。
你可以这样做来获得想要的o/p,但是你必须提到:
with open('j.txt', 'r') as f:
d =f.readlines()
for i in d:
i = i.rstrip('\n')
i+=','
print(i)
我用过rstrip
在这里,它将打印所有行,对于前三行,您可以给出一些循环或条件。
O/P 是这样的:
id = 606149,
类别名称=结构柱,
姓氏=
混凝土方柱,
类型名称 = EXIST RH C1 16 x 16,
文件=
15050 皮维结构 v2016_分离,
附件理由,位于顶部 =
最小交叉点,
图像 = 距顶部附件的偏移量 = 0,
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)