简体   繁体   English

处理多个文件并为每个文件编写一个 csv 文件

[英]Processing multiple files and write a csv file for each

I wrote a code that works fine for single file, but I have to change the names for each file.我编写了一个适用于单个文件的代码,但我必须更改每个文件的名称。 It reads a pickle file, write it into a txt file, then does some process on the context of txt file and produce a list of numbers, at the end stores the list in a dataframe and write that dataframe in csv file.它读取一个pickle文件,将其写入一个txt文件,然后对txt文件的上下文进行一些处理并生成一个数字列表,最后将列表存储在dataframe中,并将dataframe写入Z6287CB5675FFE88BAFEF.

def get_value_of_list(bit_list):
p_number = 0
for i in bit_list:
    if i == 1:
        p_number = p_number + 1
return p_number

def cross_entropy(p, q):
    return -sum([p[i] * log2(q[i]) for i in range(len(p))])

if __name__ == "__main__":

file_name = 'pickleData_AIMchat2.txt'
pickle_file = 'AIMchat2.pickle'
pk = PickleToFile(file_name, pickle_file)
pk.create_pickle_file()
h = HexToBinary(file_name)
hex_list = h.read_file()
num_of_bits = 8

scale = 16
bin_data = []
for i in hex_list:
    bin_data.append(bin(int(i, scale))[2:].zfill(num_of_bits))

my_bit_list = []
for byte in bin_data:
    bit_list = []
    for bit in byte:
        bit_list.append(int(bit))
    num_of_one_divided_by_eight = get_value_of_list(bit_list) / 8
    my_bit_list.append(num_of_one_divided_by_eight)

cross_entropy_list = []
i = 0
while i < len(my_bit_list):
    cross = cross_entropy([my_bit_list[i]], [my_bit_list[i + 1]])
    cross_entropy_list.append(cross)
    i = i + 2

df = pd.DataFrame(cross_entropy_list)
df.to_csv(r'AIMchat2.csv', index=False, index_label=False, chunksize=1000000, header=False)

I have changed create_pickle_file() to the code below to read files in the directory:我已将create_pickle_file()更改为以下代码以读取目录中的文件:

class PickleToFile:
    def __init__(self, name, pickle_file):
    self.name = name
    self.pickle_file = pickle_file

    def create_pickle_file(self):
    basepath = Path()
    files_in_basepath = basepath.iterdir('pickle/')
    for item in files_in_basepath:
        if item.is_file():
            checkThePickle = open(self.pickle_file, "rb")
            with open(self.name, 'w') as filehandler:
                for listItem in checkThePickle:
                    filehandler.write('%s\n' % listItem)

But since after reading file it writes it to a text file and then a csv file, I don't know how to do that.但是由于在读取文件后它将它写入一个文本文件,然后是一个 csv 文件,我不知道该怎么做。 Appreciate any suggestions.感谢任何建议。

If you are looking to get a list of files in directory and process them, this should get you what you want:如果您正在寻找目录中的文件列表并处理它们,这应该可以满足您的需求:

How do I list all files of a directory? 如何列出目录的所有文件?

Once you have this list of files, do a loop:获得此文件列表后,请执行循环:

for each in list_of_files:
    process_function(each)

Then, you are on your way, where 'process_function' is the function, and the argument is the filename.然后,您正在路上,其中“process_function”是 function,参数是文件名。

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

相关问题 并行处理将字典写入多个csv文件 - parallel processing write dictionary to multiple csv files 检查字符串是否存在于多个csv文件中并将行写入文件 - Check if string exists in multiple csv files and write row to file excel文件中的每两列被分成多个csv文件 - each two columns in the excel file are separated into multiple csv files 将多个 csv 文件(每个文件 – 1 个样本)组合成一个数据集 - Combine multiple csv files (each file – 1 sample) into one dataset Python入门:从CSV文件提取特定的每一行并将其写入不同的CSV文件 - Python Beginner : Extract a specific each row from CSV file and write it to different CSV files 根据内容读取一个 csv 文件并写入不同的多个 csv 文件 - Read from one csv file and write to different multiple csv files depending on content 如何将多个csv文件整理到一个csv文件中,每次都删除标题? - How to collate multiple csv files into one csv file, removing the headers each time? 每次使用pandas和python处理多个文件时,从txt文件中读取单个变量 - Read single varible from txt file each time processing multiple files using pandas and python 将每一行写入具有csv扩展名的文件 - Write each line to a file with a csv extension 为 csv 中的每一行编写一个文本文件 - Write a text file for each row in a csv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM