![](/img/trans.png)
[英]Text file converted to CSV using Python is different than converting with Excel
[英]converting text file to excel python 3
我有一个文本文件,我正在尝试将其转换为python 3中的Excel文件。该文本文件具有一系列帐户-一个文本文件如下所示:示例-
PRODUCE_NAME:abc
PRODUCE_NUMBER:12345
日期:12/1/13
PRODUCE_NAME:efg
PRODUCE_NUMBER:987
日期:2/16/16
时间:12:54:00
PRODUCE_NAME:xyz
PRODUCE_NUMBER:0046
日期:7/15/10
颜色:蓝色。
我希望excel文件看起来像这样。 在此处输入图片说明
一些代码:`#打开文本文件
op_file = open("Comp_file_1.txt", "r", encoding='windows-1252')
text_file = op_file.read()
##############################################################
# location of CAP WORD: and group them
for mj in re.finditer(r"[A-Z]\w+(:)", text_file):
col_list_start.append(mj.start(0))
col_list_end.append(mj.end(0))
col_list_group.append(mj.group())
#############################################################
# Location of the end of file and delete index 0 of start
while True:
# Advance location by 1.
location = text_file.find(".", location + 1)
# Break if not found.
if location == -1: break
# Display result.
endline = location
col_list_start.append(int(endline))
del col_list_start[0]
##############################################################
# cut out the index of the rows - abc , 12345, 12/1/13
for m in range(len(col_list_end)):
index4.append(file_data2[col_list_end[m]:col_list_start[m]])
##############################################################
# makes a data frame
# and groups the data frame
group_excel_list = {}
for k,v in zip(col_list_group, index4):
group_excel_list.setdefault(k, []).append(v)`
dataframe looks like this
key value
{"PRODUCE_NAME:": [abc, efg, xyz]}
{"PRODUCE_NUMBER:" : [12345, 987, 0046]}
{"DATE:" : [12/1/13, 2/16/16, 7/15/10]}
{"TIME:" : [12:54:00]}
{"COLOR:" [blue]}
df = pd.DataFrame(data=[group_excel_list], columns = col_list_group)
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter("Comp_file_1" + '.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
我只得到数据框的一行。 标头-PRODUCE_NAME:PRODUCE_NUMBER:DATE:第0行-[abc,efg,xyz] [12345、987、0046] [12/1 / 13、2 / 16 / 16、7 / 15/10]
您能提供的任何帮助将不胜感激。
从文本文件(.txt文件,其中的各列用制表符分隔)中读取数据,这是我的数据的情况,但当然可能与您的数据不同!):
import csv
data = []
with open("file_%02d.txt" %fileNumber, 'r') as f:
reader = csv.reader(f, dialect = 'excel', delimiter = '\t')
% reads the rows from your imported data file and appends them to a list
for row in reader:
print row
data.append(row)
将您的数据写入外部文件:
import pandas as pd
newData= pd.DataFrame(data, columns = ['name1','name2',...,'nameN'])
expData.to_csv("new_file_%02d.csv" %fileNum, sep = ';')
这或多或少是我的首要任务,但应该可以解决。 您可以写出列表中的数据,只需确保列表中的元素数和列名匹配
希望我能有所帮助!
抱歉,我不记得确切的方法,但是如果您使用f = file ...等创建文件,并将其设置为逗号分隔的值(.csv)文件,则可以直接将其加载到excel,以便所有用逗号分隔的项目都进入单独的列,而所有按Enter分隔的东西都进入单独的行(再次抱歉,我不记得确切的过程了)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.