我正在尝试从文本文件中复制用 :
取消的数据值。 文本文件具有此形式中的数据:
我有50多个文本文件包含此形式的数据:
Type: Assume
Number: 123456
Name: Assume
Phone Number: 000-000
Email Address: any@gmail.com
Mailing Address: Assume
我试图从多个文本文件中以这种格式以这种格式获取此格式:
Type Number Name Phone email Mailing Address
Assume 123456 Assume 000-000 any@gmail.com Assume
这是代码:
import re
import csv
file_h = open("out.csv","a")
csv_writer = csv.writer(file_h)
def writeHeading(file_content):
list_of_headings = []
for row in file_content:
key = str(row.split(":")[0]).strip()
list_of_headings.append(key)
csv_writer.writerow(tuple(list_of_headings))
def writeContents(file_content):
list_of_data = ['Number']
for row in file_content:
value = str(row.split(":")[1]).strip()
list_of_data.append(value)
csv_writer.writerow(tuple(list_of_data))
def convert_txt_csv(filename):
file_content = open(filename,"r").readlines()
return file_content
list_of_files = ["10002.txt","10003.txt","10004.txt"]
# for writing heading once
file_content = convert_txt_csv(list_of_files[0])
writeHeading(file_content)
# for writing contents
for file in list_of_files:
file_content = convert_txt_csv(file)
writeContents(file_content)
file_h.close()
这是以下错误:
Traceback (most recent call last):
File "Magnet.py", line 37, in <module>
writeContents(file_content)
File "Magnet.py", line 20, in writeContents
value = str(row.split(":")[1]).strip()
IndexError: list index out of range
您的代码可能在第一个文件末尾遇到空白行,或任何没有:
的行,因此,当您尝试将其拆分为键/值时它抱怨,因为它没有得到预期的列表。您可以通过检查当前行上是否有结肠来轻松修复该问题,即:
for row in file_content:
if ":" not in row: # or you can do the split and check len() of the result
continue
key = row.split(":")[0].strip()
list_of_headings.append(key)
但是...当您尝试的任务看起来非常简单时,请记住您的方法假设所有文件都相等,数字相等的key: value
组合和相同的顺序。
通过将解析的数据存储在dict
中,然后使用csv.DictWriter()
进行投标。