简体   繁体   English

如何使用 python 将 excel 的一列中的数据拆分为多列

[英]How to Split data in one column of excel into multiple column using python

Can anyone help me in in my program?任何人都可以在我的程序中帮助我吗? I have copied some data from TXT file to XLSX using XLSXwriter library, and that data copied in 1 column.我已经使用XLSXwriter库将一些数据从TXT文件复制到XLSX ,并将这些数据复制到 1 列中。 Now I would like split that data into multiple columns using space as a separator.现在我想使用空格作为分隔符将该数据拆分为多列。 Below is my program.下面是我的程序。 Now please suggest me any path forwad.现在请建议我前进的任何道路。

with open('filter_pre.txt', 'wt+') as logs_pre:
    logs_pre.write(filter_pre)

with open('filter_pre.txt', 'rt+') as Pre_logs:
    lines = Pre_logs.readlines()
    for line in lines:
        Pre_filter_logs.write(row, col, line.strip())
        row += 1
        if not line:
            break
    filter_logs.close()

Writing an answer for I can't comment;为我无法评论写一个答案;

I think you should split the data before writing to the XLSX.我认为您应该在写入 XLSX 之前拆分数据。 It's much easier.这要容易得多。 The Office suite is notorious for being hard to interact with in code. Office 套件因难以在代码中交互而臭名昭著。

with open("inputs.txt") as f:
    rowcount = 0
    for row in f.readlines():
        row = row.strip() # Clean up each row
        # Reset the column counter for each row. 
        # Remove the line below and see what happens ;) 
        columcount = 0 
        for column in row.split(" "): # Break up a row on each space.
            excelsheet.write(rowcount, columcount, column)
            columcount +=1 
        rowcount += 1

You can add additional function (addRow) to the Worksheet object somewhere in you code.您可以在代码中的某处将额外的 function (addRow) 添加到工作表 object 中。

def addRowToXls(self, data, row = 0):
    for colNum, value in enumerate(data):
            self.write(row, colNum, value)

So you can add rows (no matter how many columns in it) to a worksheet easily, like:因此,您可以轻松地将行(无论其中有多少列)添加到工作表中,例如:

worksheet.addRow(data = ["Column 1 text", "Column 2 text", "And so on..."], row = 3) # add to row 4

So in your case the whole code would be like this I guess所以在你的情况下,我猜整个代码都是这样的

import xlsxwriter


def addRowToXls(self, data, row = 0):
    for colNum, value in enumerate(data):
            self.write(row, colNum, value)

xlsxwriter.worksheet.Worksheet.addRow = addRowToXls

workbook = xlsxwriter.Workbook("new_file.xlsx") # new xlsx file
worksheet = workbook.add_worksheet()
worksheet.addRow(data = ["column 1 header text", "column 2 header text", "and so on..."]) # you can skip this
row_save = 1 # start from 0, if you skip column headers

with open('filter_pre.txt', 'rt+') as Pre_logs: # text file to read from
    lines = Pre_logs.readlines()
    for line in lines:
        worksheet.addRow(data = line.split(" "), row = row_save)  # used space as seperator
        row_save += 1

workbook.close()

Tested.经测试。 Works as supposed to.按预期工作。

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

相关问题 如何使用 sql 查询/Python 将一列数据拆分为多列? - How to split one column data into multiple columns using sql query/Python? 如何在python中使用标题将一列拆分为多列? - how to split one column to multiple column with header in python? 在 python 如何将数据集列拆分为多列 - In python how split the dataset column to multiple column 如何使用 python 脚本将一行中存在的数据(以空格分隔)拆分为同一 excel 工作表中的不同列? - How to split data present in a row (separated by space) into different column in the same excel sheet using python script? 在Python中将一个字符串列拆分为多个列 - split one string column to multiple columns in Python 在 Python 中将一列拆分为多列 - Split one column into multiple columns in Python 如何使用熊猫按国家/地区列将数据拆分为一个工作簿中的多个工作表 - How to Split data into multiple sheets in one single workbook by country column using pandas 如何使用python拆分列 - how to split column using python 如何使用正则表达式将一列拆分为Pandas中的多列? - How to split one column into multiple columns in Pandas using regular expression? 如何使用python根据一列将整个数据集分为4个范围 - How to split the whole dataset into 4 range based on one column using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM