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

2024-03-28 11:21:32 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我有一个用引号括起来的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"

Tags: csvtopgearboba5fccreamice
1条回答
网友
1楼 · 发布于 2024-03-28 11:21:32

某些东西(注意,这可能需要稍微编辑一下,但您的问题中没有给出代码)例如:

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()

相关问题 更多 >