2010-10-21 59 views
2

我想创建一个python脚本,将围绕一个字符串的一部分报价封装字符串的一部分,经过3逗号使用python后3个逗号

因此,如果输入的数据是这样的:

1234,1,1/1/2010,This is a test. One, two, three. 

我想蟒蛇字符串转换:

1234,1,1/1/2010,"This is a test. One, two, three." 

的报价总是需要经过3个逗号添加

我使用Python 3.1.2,并迄今以下:当我尝试运行它,并不会产生输出

你可以看到什么是错的

i_file=open("input.csv","r") 
o_file=open("output.csv","w") 

for line in i_file: 
     tokens=line.split(",") 
     count=0 
     new_line="" 
     for element in tokens: 
       if count = "3": 
         new_line = new_line + '"' + element + '"' 
         break 
       else: 
         new_line = new_line + element + "," 
         count=count+1 

     o_file.write(new_line + "\n") 
     print(line, " -> ", new_line) 

i_file.close() 
o_file.close() 

脚本立即关闭?

感谢

+3

'if count =“3”:'的语法错误。为什么'3'在引号中? – bernie 2010-10-21 04:22:19

回答

2

已经处理了我的评论中提到的两个问题上面我刚刚测试了下面的代码(编辑:几乎工程;见下文很短的代码示例的全面测试和工作版本)为您测试输入。

i_file=open("input.csv","r") 
o_file=open("output.csv","w") 

for line in i_file: 
    tokens=line.split(",") 
    count=0 
    new_line="" 
    for element in tokens: 
     if count == 3: 
      new_line = new_line + '"' + element + '"' 
      break 
     else: 
      new_line = new_line + element + "," 
      count=count+1 

    o_file.write(new_line + "\n") 
    print(line, " -> ", new_line) 

i_file.close() 
o_file.close() 

边注:在Python
一个相对较新的特点是the with statement。以下是你可能会如何利用编码(请注意,您不需要在处理的末尾添加close()呼叫)的更健壮的方法的优点的例子:

with open("input.csv","r") as i_file, open("output.csv","w") as o_file: 
    for line in i_file: 
     tokens = line.split(",", 3) 
     if len(tokens) > 3: 
      o_file.write(','.join(tokens[0:3])) 
      o_file.write('"{0}"'.format(tokens[-1].rstrip('\n'))) 
+1

谢谢,这是很棒的信息。我希望我能给你所有的复选标记 – samJL 2010-10-21 07:22:05

2

较短,但未经测试:

i_file=open("input.csv","r") 
o_file=open("output.csv","w") 

comma = ',' 
for line in i_file: 
    tokens=line.split(",") 
    new_line = comma.join(tokens[:3]+['"'+comma.join(tokens[3:])+'"']) 
    o_file.write(new_line+'\n') 
    print(line, " -> ", new_line) 

i_file.close() 
o_file.close() 
1

也许你应该考虑使用正则表达式来做到这一点? 喜欢的东西

import re 
t = "1234,1,1/1/2010,This is a test. One, two, three." 
first,rest = re.search(r'([^,]+,[^,]+,[^,]+,)(.*)',t).groups() 
op = '%s"%s"'%(first,rest) 
print op 

1234,1,1/1/2010,"This is a test. One, two, three." 

这是否满足你的要求吗?

1
>>> import re 
>>> s 
'1234,1,1/1/2010,This is a test. One, two, three.' 
>>> re.sub("(.[^,]*,.[^,]*,.[^,]*,)(.*)" , '\\1\"\\2"' , s) 
'1234,1,1/1/2010,"This is a test. One, two, three."' 

import re 
o=open("output.csv","w") 
for line in open("input.csv"): 
    line=re.sub("(.[^,]*,.[^,]*,.[^,]*,)(.*)" , '\\1\"\\2"' , line) 
    o.write(line) 
o.close() 
+0

+1。 're.sub'。这就是我一直在寻找的。 – 2010-10-21 06:49:23

+0

我喜欢人们如何投票给对方的答案。这是一个伟大的社区。 – bernie 2010-10-21 07:05:36