使用python和panda excel数据表到文本文件

发布于 2025-02-04 18:54:06 字数 2759 浏览 1 评论 0原文

我在一个工作项目中充分利用熊猫遇到了一些麻烦。简而言之,我必须从Excel中获取所有数据,并以其他设备可以正确阅读的方式格式化它。我已经将所有需要的列推入数据缓冲区,但是我需要检查一个数据缓冲区并打印一个不同的命令(即,在该列中,它说明了健康:我需要先在输出文件中打印Heal Heal ,如果说不健康,我需要打印联合国,但是,如果它说其他任何内容,我需要从数据中删除这一行,也不是健康/不健康的,他们可能只有其他单词,但是我正在寻找的关键部分是这些作品被包含。)我将附上我正在查看的Excel/Output Text示例。

到目前为止,在我的代码中,我已经隔离了所需的列,并跳过了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. 

示例excel表

我检查项目是否属于两个想要的类别需要删除类别)我需要使第一列成为命令。

这是文本文件中最终输出的一个示例,

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       ;

它的结构不完全和ID。 它必须是文本文件,因为机器读取文本

我最大的问题是如何在右侧读取第二个类别的第二类,请检查是否删除该行并在文本文件的左侧空间上打印一些命令。

(我还需要在第二次通过的第二次通过时为我关心的项目进行价格,我必须生成文件的单独部分。)

此外,对于需要更改的数据,我必须在某些IE shelf323之后读取第一个数字 我知道并转换为物理地址的前3个需要放入表格中,而23就像架子的行数一样。这些需要以格式打印到最终TXT。

LMK如果我能澄清任何东西。我的Python技能并不令人惊讶,但我正在尝试完成此操作。

这样做的目的是自动读取Excel文件并将其转换为可以通过特定机器读取的TXT文件。

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. 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.

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.

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

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

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.
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. and these need to get printed to the final txt in a format.

LMK if i can clarify anything. My python skills aren't amazing but I am trying to get this done.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2025-02-11 18:54:06

尝试此代码查看是否有效。以下代码将使用A |将您的每个Excel选项卡转换为TXT文件划界。

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)

Try this code see if it works. Below code will convert each of your Excel tab to txt file with a | 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文