[英]separating column data into two new columns in a csv file
Good day all, 大家好
I am writing into a csv file and I need to separate one column's information into two new columns for example from this: 我正在写入一个csv文件,我需要将一个列的信息分成两个新列,例如:
Call
Goku (1000)
To have something that looks like this: 要具有以下内容:
Name Landline
Goku 1000
so I need help on removing the brackets around the number and only taking the numbers inside it while renaming the Call column heading to Name and then create new column called Landline before writing the other columns, Name and Landline need to be the first two columns then the others follow. 因此,我需要删除数字上的方括号并仅取其中的数字,同时将“呼叫”列标题重命名为“名称”,然后在编写其他列之前创建一个名为“座机”的新列,“名称”和“座机”必须是前两列,然后其他人跟随。
I have the following code: 我有以下代码:
infile = "path to file"
outfile = "path to file"
def csv_wr():
# Opens output file for writing data into.
of = open(outfile, 'w')
# Reads the data from input file
with open(infile, 'r') as f:
data = csv.DictReader(f)
count = 0
# Writing data into output file.
for row in data:
# Converts the date column.
mydate = datetime.strptime(row['date'], '%d %b %Y %H:%M: %S').strftime('%Y-%m-%d %H:%M:%S')
x = ''.join(row['value'])
# Inserting '0' value to empty columns.
if len(x) < 1:
value = 0
else:
value = row['value']
# If statement inserting data with commas except for the last row into the output file.
if count > 0:
textline = ",\n('{0}','{1}','{2}','{3}','{4}','{5}','{6}')".format(row['call'], row['to'], mydate,
row['duration'], row['bill'],
value, row['status'])
else:
textline = "('{0}','{1}','{2}','{3}','{4}','{5}','{6}')".format(row['call'], row['to'], mydate,
row['duration'], row['bill'],
value, row['status'])
of.write(textline)
It would be best to make use of a csv.DictWriter()
to create your CSV file. 最好利用csv.DictWriter()
创建CSV文件。 The call
field could be split using a regular expression to get the name
and landline
parts. 可以使用正则表达式拆分call
字段,以获取name
和landline
部分。 This can then be added to the dictionary and written using the .writerow()
call: 然后可以将其添加到字典中,并使用.writerow()
调用来编写:
from collections import defaultdict
from datetime import datetime
import csv
import re
in_file = 'input.csv'
out_file = 'output.csv'
fieldnames = ['date', 'name', 'landline', 'to', 'duration', 'bill', 'status']
with open(in_file, 'r', newline='') as f_input, open(out_file, 'w', newline='') as f_output:
csv_input = csv.DictReader(f_input)
csv_output = csv.DictWriter(f_output, fieldnames=fieldnames, extrasaction='ignore')
csv_output.writeheader()
for row in csv_input:
name, landline = re.match(r'(.*?) \((.*?)\)', row['call']).groups()
row['name'] = name
row['landline'] = landline
row['date'] = datetime.strptime(row['date'], '%d %b %Y %H:%M: %S').strftime('%Y-%m-%d %H:%M:%S')
csv_output.writerow(row)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.