【发布时间】:2015-12-29 05:52:00
【问题描述】:
我做错了什么?
Input file data format :
Address: data modified Phone: 7373737373
Eligibility: Age: 12
Sex: MALE
Race:
#Test file
content = open("output.txt","r")
# Output file
fileCSV = open("testCSV.csv","a")
patAddress = re.compile('Address:[\w \W \S \d \D - / + , = \s ]{40}')
patAge = re.compile('Age:[0-9 \s]{1,3}')
patSex = re.compile('Sex:[a-z A-Z \s \S]{1,2}')
for value in content:
address = patAddress.findall(value)
sex = patSex.findall(value)
age = patAge.findall(value)
for p_address in address:
fileCSV.write(p_address)
for p_sex in sex:
fileCSV.write("|"+p_sex)
for p_age in age:
fileCSV.write("|"+p_age)
fileCSV.write("\n")
fileCSV.close();
我希望输出是这样的:
Address : some text| Sex: M|Age: 25 \n in the end
我得到的输出是:
Address : some text|Age: 25
|Sex: M
这也是我在 Sex 之后得到的确切输出。
谁能告诉我这背后的原因。这是python的第三天,我的意思是我是python的新手。我无法得到打印性别之前的年龄背后的原因
感谢任何帮助。
【问题讨论】:
-
你能展示你的示例输入吗?
-
只有我能分享这么多,那个 output.txt 是包含大量数据的文件。我需要将其转换为 csv(仅必填字段)。
标签: python regex python-2.7