While trying to copy data Next to variables from text to csv with python getting error:
While trying to copy data Next to variables from text to csv with python getting error:
我正在尝试从文本文件中复制用 :
分隔的数据值。
具有如下形式数据的文本文件:
I have 50+ text file contains data in this form:
Type: Assume
Number: 123456
Name: Assume
Phone Number: 000-000
Email Address: any@gmail.com
Mailing Address: Assume
我正在尝试从多个文本文件中获取 csv 格式的数据值:
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
您的代码可能会在第一个文件的末尾遇到空行,或者其中没有 :
的任何行,因此当您尝试将其拆分为 key/values 时它抱怨说它没有得到预期长度的列表。您可以通过检查当前行上是否有冒号来轻松解决该问题,即:
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()
进行出价,效果会更好。
我正在尝试从文本文件中复制用 :
分隔的数据值。
具有如下形式数据的文本文件:
I have 50+ text file contains data in this form:
Type: Assume
Number: 123456
Name: Assume
Phone Number: 000-000
Email Address: any@gmail.com
Mailing Address: Assume
我正在尝试从多个文本文件中获取 csv 格式的数据值:
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
您的代码可能会在第一个文件的末尾遇到空行,或者其中没有 :
的任何行,因此当您尝试将其拆分为 key/values 时它抱怨说它没有得到预期长度的列表。您可以通过检查当前行上是否有冒号来轻松解决该问题,即:
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()
进行出价,效果会更好。