简体   繁体   English

想要读取一个文件并使用 python 写入现有的 excel 表

[英]Want to read an a file and write to existing excel sheet using python

[enter image description here][1]I have a text file with data delimited qith a pipe delimiter, i want to write it to an excel sheet. [在此处输入图像描述][1]我有一个文本文件,其中数据以管道分隔符分隔,我想将其写入 Excel 工作表。

I have created a code but it is taking around 700 seconds to execute since the data in the input file is large(about 45kb).我已经创建了一个代码,但由于输入文件中的数据很大(大约 45kb),因此执行大约需要 700 秒。 Is there any way to optimize it?有什么办法可以优化吗?

Thanks谢谢

Please find the code as follows:请查找代码如下:

import csv
from time import process_time


def create_sheet():
    wb1 = Workbook()
    wb1.create_sheet("A")
    wb1.create_sheet("B")
    src_sheet = wb1.create_sheet("C")
    print(type(src_sheet))
    wb1.create_sheet("D")
    sheet = wb1['Sheet']
    wb1.remove(sheet)
    write_Data(src_sheet)
    wb1.save('outfile.xlsx')


def write_Data(src_sheet):
    csv.register_dialect('myDialect', delimiter='|', quoting=csv.QUOTE_ALL)
    data_list = []
    with open("C:/Users/atapadar/input_text.txt",
              "r") as csvfile:
        reader = csv.reader(csvfile, dialect='myDialect')
        count: int = 1
        for i in reader:
            if count == 1:
                i.append("New col")
            else:
                i.append(i[0] + i[1] + i[3])
            count = count + 1
            src_sheet.append(i)



t1_start = process_time()

create_sheet()

t1_stop = process_time()
print("Elapsed time during the whole program in seconds:", t1_stop - t1_start)


  [1]: https://i.stack.imgur.com/X9iMQ.png

Doing this via dataframes would be much simpler:通过数据帧执行此操作会简单得多:

df = pd.read_csv("filename.csv", sep="|")
df['New col'] = int(df['C1']) + int(df['C2']) + int(df['C3'])

df.to_excel("output.xlsx", sheet_name='Sheet_name_1') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM