【问题标题】:Excel Data sheet to a text file using python and panda使用 python 和 panda 将 Excel 数据表转换为文本文件
【发布时间】:2022-08-17 14:16:21
【问题描述】:

我在完全利用熊猫进行工作项目时遇到了一些麻烦。简而言之,我必须从 excel 中获取所有数据并以其他设备可以正确读取的方式对其进行格式化。我已经将我需要的所有列推入数据缓冲区,但是我需要检查其中一个数据缓冲区并打印不同的命令( IE 在该列中它说健康:我需要先在输出文件中打印 HEAL ,如果它说不健康,我需要打印 UN 但是如果它说其他我需要从数据中删除该行,健康/不健康也不会只存在他们可能有其他词,但我正在寻找的关键部分是如果这些部分包含在内。)我将附上我正在查看的示例 excel/输出文本。

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

在我检查该项目是否属于两个想要的类别之后(除了想要的类别之外的所有内容都需要删除行),我需要将第一列设为命令。

这是最终输出的示例

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文件。

    标签: python excel pandas automation hardware


    【解决方案1】:

    试试这个代码看看它是否有效。下面的代码会将您的每个 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['sheet'] = name
        sheet.drop(sheet.columns[[-1,]], axis=1, inplace=True)
        ExcelSheet = ExcelSheet.append(sheet)   
        sheet.to_csv (name+'.txt', '|', index = None, header=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多