Python逐行从csv中提取一个和两个单词的句子并将其写入其他语句

时间:2014-05-14 02:48:28

标签: python regex csv line-by-line

嗨我有一个带引号句子的csv,我想省略任何不超过三个单词的句子,并逐行复制到另一个csv。所有帮助高度赞赏。 感谢

Input csv:

"9795e7dc9a5b032bdb39ace56c08b0e1","Top     Gear","FC Barcelona","Making code names so people dont know who your talking about","Audi A5","Ice cream","Wentworth Miller","Bob Marley","Megan Fox","FIFA","ShootandGoal","Eminem","Nike","Manchester United","Pilotta"
"650c7b5f671972947ef34de59a8e9dd3","Tioga Downs Casino","Ryan Gosling","Crazy, Stupid, Love.","Jane Eyre","Boycott Nike for Resigning Michael Vick"

 Output csv:
 "9795e7dc9a5b032bdb39ace56c08b0e1","Top     Gear","FC Barcelona","Audi A5","Ice cream","Wentworth Miller","Bob Marley","Megan Fox","FIFA","ShootandGoal","Eminem","Nike","Manchester United","Pilotta"
"650c7b5f671972947ef34de59a8e9dd3","Tioga Downs Casino","Ryan Gosling","Jane Eyre"

1 个答案:

答案 0 :(得分:0)

Something (请注意,这可能需要稍微编辑,但您的问题中没有代码可以使用),例如:

newfile = open(newfilename,"w")  
oldfile = open(oldfilename).readlines()


for line in oldfile:
  items = line.split(",")#gets each quoted thing
  for i in items:
      subitems = i.split() #will return a list of each word inside each quoted thing
      if len(subitems) <= 2:
          newfile.write(i + ",")


newfile.close()