我有一个CSV文件,我希望文件中每个名字的第一个字母大写,其他名称是小写。我试图使用.title()来完成这个。我希望使用大写字母(CleanNames.csv)的CSV文件将会拉动'来自另一个CSV(ValidNames.csv)的这些名称是“拉动”的。来自无组织名称列表中的那些名称(10000DirtyNames.csv)。
这是我到目前为止所做的:
import re
import csv
with open("10000DirtyNames.csv", "r") as file:
with open('ValidNames.csv', 'w+') as ValidNames_file:
write = csv.writer(ValidNames_file, delimiter=',');
data = file.read();
pattern = "[A-Za-z]{1,}";
search = re.findall(pattern, data);
write.writerow(search);
with open('CleanNames.csv', 'w') as CleanNames_file:
write2 = csv.writer(CleanNames_file, delimiter=',');
data2 = ValidNames_file.read();
write2.writerow(data2.title());
除了CleanName.csv根本没有填充之外它可以工作。没有错误消息。我做错了什么?
答案 0 :(得分:0)
我自己想出来,但我想我会发布我的解决方案,包括解决类似问题所需的人。
import re
import csv
with open("10000DirtyNames.csv", "r") as file:
with open('ValidNames.csv', 'w+') as ValidNames_file:
write = csv.writer(ValidNames_file, delimiter=',');
data = file.read();
pattern = "[A-Za-z]{1,}";
search = re.findall(pattern, data);
write.writerow(search);
with open('ValidNames.csv') as ValidNames_file, open('CleanNames.csv', 'w') as CleanNames_file:
for name in ValidNames_file:
CleanNames_file.write(name.title())