繁体   English   中英

python csv 拆分行值和新列

[英]python csv split row value, and new column

我有一个 csv 文件,

    a         b              c
   text      text        2020/15 file  
   text      text        2020/12 file  
   text      text        2020/37 file  
   text      text        2020/25 file  

我想要 output (替换正则表达式拆分行值和新列),

    a         b              c             new column
   text      text        2020/15 file         15
   text      text        2020/12 file         12 
   text      text        2020/37 file         37
   text      text        2020/25 file         25

我该怎么做,请帮助我,谢谢

此代码将满足您的需要。 读取名为 old.csv 的 csv 并在同一文件夹中以您的格式创建 new.csv。

马克解决了,在这里问任何问题之前先做一些锻炼....

import csv,re
def find_number(text, c):
    return re.findall(r'%s(\d+)' % c, text)
col1, col2, col3, col4 = [], [], [], []
with open("Old.csv",'rt')as f: #csv file name
  data = csv.reader(f)
  for row in data:
      col1.append(row[0])
      col2.append(row[1])
      col3.append(row[2])
for _ in col3:#extracting /
    col4.extend(find_number(_, "/"))

rows = zip(col1,col2,col3,col4)
with open('New.csv', 'w') as csvfile:
    csvwriter = csv.writer(csvfile)
    for row in rows:
        csvwriter.writerow(row)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM