我正在尝试将一列中存在的数据拆分并将其存储到新列中
**Inputdata.csv**
Braund, Mr. Owen Harris ,1
Heikkinen, Miss. Laina ,0
Allen, Mr. William Henry ,0
**Expecting_output.csv**
Braund,Owen Harris ,1,Mr
Heikkinen,Laina ,0,Miss
Allen,William Henry ,0,Mr
我已经尝试过并获得了查找和替换功能,但无法对查找进行编码并将其存储到新列中
import csv
print(dir(csv))
filename = "H:\\FairDealCustomerData.csv"
csvout = "H:\\FairDealCustomerDataOUT.csv"
with open(filename,"r",newline='') as file:
file = ''.join([i for i in file]).replace("Mr.", "")
file = ''.join([i for i in file]).replace("Miss.", "")
file = ''.join([i for i in file]).replace("Mrs.", "")
with open(csvout,"w",newline='') as outfile:
outfile.writelines(file)
outfile.close()
Inputdata.csv
Braund, Mr. Owen Harris ,1
Heikkinen, Miss. Laina ,0
Allen, Mr. William Henry ,0
Expecting_output.csv
Braund,Owen Harris ,1,Mr
Heikkinen,Laina ,0,Miss
Allen,William Henry ,0,Mr
答案 0 :(得分:0)
使用正则表达式提取不同的组,然后根据需要对其重新排序。
import re
new_lines = []
with open('inputdata.txt', 'r') as file:
for line in file:
regex = re.compile(r'(\w+),\s?(Mr.|Miss.|Mrs.)\s(\w+\s?\w*\s),([01])')
new_line = regex.findall(line)[0]
print(f"{new_line[0]},{new_line[2]},{new_line[3]},{new_line[1]}")
结果:
Braund,Owen Harris ,1,Mr.
Heikkinen,Laina ,0,Miss.
Allen,William Henry ,0,Mr.
答案 1 :(得分:0)
尝试一下,输出与您期望的相同。
import csv
filename = "H:\\FairDealCustomerData.csv"
csvout = "H:\\FairDealCustomerDataOUT.csv"
with open(filename, 'r', newline='') as read_file:
readCSV = csv.reader(read_file, delimiter=',')
with open(csvout, 'w', newline='') as write_file:
writer = csv.writer(write_file)
for row in readCSV:
writer.writerow([row[0], row[1].split('.')[1].strip() + ' ', row[2], row[1].split('.')[0].strip()])
答案 2 :(得分:0)
万岁,我感谢@Kushan Gunasekera
Process[] procs = Process.GetProcessesByName("torch");
foreach (Process proc in procs)
{
proc.WaitForInputIdle();
}
System.Threading.Thread.Sleep(10000);
foreach (Process proc in procs)
{
proc.Kill();
// ... or ... (depending on the app)
proc.CloseMainWindow();
}