简体   繁体   English

Excel 使用 python 和 panda 将数据表转换为文本文件

[英]Excel Data sheet to a text file using python and panda

I was having some trouble fully utilizing panda for a work project.我在完全利用熊猫进行工作项目时遇到了一些麻烦。 In short I have to take all the data from excel and format it in a way another device can properly read.简而言之,我必须从 excel 中获取所有数据,并以其他设备可以正确读取的方式对其进行格式化。 I have already shoved all the columns that I need into a data buffer, however I need to check one of the data buffer and print a different command ( IE in that column it says healthy: I would need to print HEAL first in the output file, if it says unhealthy i would need to print UN however if it says anything else I would need to delete the row from data, Also healthy/ unhealthy wont be there exclusively they may have other words but the key part I am looking for is if those pieces are contained.) I will attach example excel/output text that I am looking at.我已经将我需要的所有列推入数据缓冲区,但是我需要检查一个数据缓冲区并打印一个不同的命令( IE 在该列中它说健康:我需要首先在 output 文件中打印 HEAL ,如果它说不健康,我需要打印 UN 但是如果它说其他我需要从数据中删除该行,健康/不健康也不会只存在他们可能有其他词,但我正在寻找的关键部分是如果这些部分包含在内。)我将附上我正在查看的示例 excel/输出文本。

So far in my code I have isolated the columns I want and have skipped the extra white space rows that the excel file will have and I also terminated with a semicolon and new line character.到目前为止,在我的代码中,我已经隔离了我想要的列,并跳过了 excel 文件将具有的额外空白行,并且我还以分号和换行符结尾。

import pandas as pd 
import numpy as np





#file_name = input("Please input a file to read. It should have a name like File.xlsm\n")
file_name = "file.xlsm"

# maybe add a part where if it fails you ask the user again 




read_file = pd.read_excel(file_name, sheet_name = 0, header = 0, index_col = 0, usecols = [" Name", "Comment", "Price", "category", "data to change"], skiprows = 15) # sheet is equal to 0 by default os it will do the first one 



#print("\n")

#print(read_file)




# search fe 
#Fruit Name | Comment | Price | Category | Aisle# / data  




#for index, row in read_file.iterrows():  trying to find if healthy or unhealthy or to remove row
#    if cell = Dgiit\


#read_file["Fruit Name"] = read_file["Fruit Name"].str.lower() #broken. tring to get name in to paranthees and all lower case. APPLE -> "apple" 

#drop_val = #!digital / supply    
#read_file = read_file[~read_file['A'].isin(drop_val)] ! ( unhealty * | *Healthy )


# saving to a text file 

read_file.to_csv('input2.txt', sep = '\t', line_terminator = ';\n') # saves data frame to tab seperated text file. need to find out how to have semi colons at the end. 

Sample Excel Sheet样品 Excel 表

After I check whether the item is on of two wanted categories ( everything besides the wanted categories need to be row removed ) I need to make the first column be a command.在我检查该项目是否属于两个想要的类别之后(除了想要的类别之外的所有内容都需要删除行),我需要将第一列设为命令。

Heres an example of the final output下面是最终 output 的示例

HEALTHY "bannana" "Aisle#-storename" ; // the comment I need from the comment box //(the number comes from data that needs to be manipulated tab, it has some exess info and things i need to conver)
HEALTHY "orange" "Aisle#-storename"; // what came first the color or the fruit. is the fruit named after the color or the color after the fruit
UNHEALTHY "cupcake" "Aisle#-storename"; // not good for you but maybe for the sould 
UNHEALTHY "pizza" "Aisle#-storename";



Here is what I am getting 

Name      Comment   Price   Category    Data;
BANANNA    x           x         x        x ;
APPLE       x          x          x          x;
ORANGE       x          x          x        x       ;

in the text file its not quite aligned and id like for it to be more structed.在文本文件中,它并没有完全对齐,并且 id 喜欢它更有条理。 It has to a text file because the machine reads a text它必须是文本文件,因为机器读取文本

My biggest question is how do I read the the 2nd to last category on the right check whether to remove the row and print some command on the LEFT most space of the text file.我最大的问题是如何读取右侧倒数第二个类别,检查是否删除该行并在文本文件的最左侧空间打印一些命令。

( also i need to do price on teh second pass for the items i care about, i have to generate a separate part of the file. ) (我还需要对我关心的项目进行第二次定价,我必须生成文件的单独部分。)

Also for data that needs to change I have to read the first number after some IE SHELF323 the first 3 needs to be put in the table that i know and converted to a physical address and the 23 is like the number of rows the shelf has.同样对于需要更改的数据,我必须在一些 IE SHELF323 之后读取第一个数字,前 3 个需要放入我知道的表中并转换为物理地址,而 23 就像架子的行数一样。 and these need to get printed to the final txt in a format.这些需要以某种格式打印到最终的txt中。

LMK if i can clarify anything. LMK,如果我能澄清任何事情。 My python skills aren't amazing but I am trying to get this done.我的 python 技能并不出色,但我正在努力完成这项工作。

The purpose of this is to automate teh reading of an excel file and convert it to a txt file that can be read by a specific machine.这样做的目的是自动读取 excel 文件并将其转换为可由特定机器读取的 txt 文件。

Try this code see if it works.试试这个代码看看它是否有效。 Below code will convert each of your Excel tab to txt file with a |下面的代码会将您的每个 Excel 选项卡转换为带有 | 的 txt 文件。 delimited.划定的。

import pandas as pd
 
sheets_dict = pd.read_excel(r'C:\my_file.xlsx', sheet_name=None)

ExcelSheet = pd.DataFrame()
for name, sheet in sheets_dict.items():   
sheet.to_csv (name+'.txt', '|', index = None, header=True)

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

相关问题 如何使用 python 熊猫仅打印到 excel 工作表中的一列 - How to print to only one column in an excel sheet using python panda 如何在python中使用panda在现有excel表中追加列 - how to append columns in existing excel sheet using panda in python 使用python将文本文件(对象)作为.txt插入到Excel工作表的单元格中 - Insert a text file (object) as .txt into a cell of excel sheet using python 使用 Python 替换文件中 Excel 表中的文本 - Replace the text from an Excel Sheet in a File Using Python 从一个文件中读取字典并使用 python 从熊猫创建一个 excel(数据框)l - read dictionaries from one file and create an excel (data frame)l from panda using python 如何使用熊猫读取文本文件作为输入并写入Excel列? - How to Read Text file as input and write into excel columns using panda? 如何使用 Python 从多个文本文件中提取数据到 Excel? (每张纸一个文件的数据) - How do I extract data from multiple text files to Excel using Python? (One file's data per sheet) Python 熊猫使用不同日期读取多个 excel 文件 - Python Panda reading multiple excel file using different date 使用 Excel 工作表中的数据在 python 中绘制图形 - Using data from an excel sheet to graph in python 如何将已完成的熊猫移动到Excel文件或Google表格中 - How to move completed Panda into a excel file or a google sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM