简体   繁体   English

将每个 Excel 电子表格行与标题保存在单独的 .txt 文件中(保存为参数样本以供模拟程序读取)

[英]Save each Excel-spreadsheet-row with header in separate .txt-file (saved as a parameter-sample to be read by simulation programs)

I'm a building energy simulation modeller with an Excel-question to enable automated large-scale simulations using parameter samples (samples generated using Monte Carlo).我是一个建筑能源模拟建模师,有一个 Excel 问题,可以使用参数样本(使用蒙特卡罗生成的样本)进行自动化大规模模拟。 Now I have the following question in saving my samples:现在我在保存样本时遇到以下问题:

I want to save each row of an Excel-spreadsheet in a separate .txt-file in a 'special' way to be read by simulation programs.我想以一种“特殊”的方式将 Excel 电子表格的每一行保存在一个单独的 .txt 文件中,以供模拟程序读取。

Let's say, I have the following excel-file with 4 parameters (a,b,c,d) and 20 values underneath:假设我有以下 excel 文件,其中包含 4 个参数(a、b、c、d)和下面的 20 个值:

a    b    c    d
2    3    5    7
6    7    9    1
3    2    6    2  
5    8    7    6
6    2    3    4

Each row of this spreadsheet represents a simulation-parameter-sample.该电子表格的每一行代表一个模拟参数样本。 I want to store each row in a separate .txt-file as follows (so 5 '.txt'-files for this spreadsheet):我想将每一行存储在一个单独的 .txt 文件中,如下所示(因此该电子表格有 5 个“.txt”文件):

'1.txt' should contain: “1.txt”应包含:

a=2;
b=3;
c=5;
d=7;

'2.txt' should contain: “2.txt”应包含:

a=6;
b=7;
c=9;
d=1;

and so on for files '3.txt', '4.txt' and '5.txt'.对于文件“3.txt”、“4.txt”和“5.txt”,依此类推。

So basically matching the header with its corresponding value underneath for each row in a separate .txt-file ('header equals value;').因此,对于单独的 .txt 文件中的每一行,基本上将标题与其对应的值进行匹配(“标题等于值;”)。

Is there an Excel add-in that does this or is it better to use some VBA-code?是否有执行此操作的 Excel 加载项,还是使用某些 VBA 代码更好? Anybody some idea?有人有什么想法吗?

(I'm quit experienced in simulation modelling but not in programming, therefore this rather easy parameter-sample-saving question in Excel. (Solutions in Python are also welcome if that's easier for you people)) (我在模拟建模方面没有经验,但在编程方面没有经验,因此在 Excel 中这是一个相当简单的参数样本保存问题。(如果对你们来说更容易的话,也欢迎 Python 中的解决方案))

my idea would be to use Python along with Pandas as it's one of the most flexible solutions, as your use case might expand in the future.我的想法是将PythonPandas一起使用,因为它是最灵活的解决方案之一,因为您的用例将来可能会扩展。

I'm gonna try making this as simple as possible.我会尽量让这件事变得简单。 Though I'm assuming, that you have Python, that you know how to install packages via pip or conda and are ready to run a python script on whatever system you are using.尽管我假设你有 Python,你知道如何通过pipconda安装包,并准备好在你使用的任何系统上运行 python 脚本。

First your script needs to import pandas and read the file into a DataFrame :首先,您的脚本需要导入pandas并将文件读入DataFrame

import pandas as pd

df = pd.read_xlsx('path/to/your/file.xlsx')

(Note that you might need to install the xlrd package, in addition to pandas ) (请注意,除了pandas之外,您可能还需要安装xlrd包)

Now you have a powerful data structure, that you can manipulate in plenty of ways.现在您拥有了一个强大的数据结构,您可以通过多种方式对其进行操作。 I guess the most intuitive one, would be to loop over all items.我想最直观的方法是遍历所有项目。 Use string formatting, which is best explained over here and put the strings together the way you need them:使用字符串格式,最好在此处解释并按照您需要的方式将字符串组合在一起:

outputs = {}

for row in df.index:
    s = ""
    for col in df.columns:
        s += "{}={};\n".format(col, df[col][row])
    print(s)

now you just need to write to a file using python's io method open .现在你只需要使用 python 的 io 方法open写入文件。 I'll just name the files by the index of the row, but this solution will overwrite older text files, created by earlier runs of this script.我将仅按行的索引命名文件,但此解决方案将覆盖由早期运行此脚本创建的旧文本文件。 You might wonna add something unique like the date and time or the name of the file you read to it or increment the file name further with multiple runs of the script, for example like this .您可能会添加一些独特的内容,例如日期和时间或您读取的文件的名称,或者通过多次运行脚本进一步增加文件名,例如像这样.

All together we get:我们一起得到:

import pandas as pd

df = pd.read_excel('path/to/your/file.xlsx')
file_count = 0

for row in df.index:
    s = ""
    for col in df.columns:
        s += "{}={};\n".format(col, df[col][row])

    file = open('test_{:03}.txt'.format(file_count), "w")
    file.write(s)
    file.close()

    file_count += 1

Note that it's probably not the most elegant way and that there are one liners out there, but since you are not a programmer I thought you might prefer a more intuitive way, that you can tweak yourself easily.请注意,这可能不是最优雅的方式,并且有一个衬里,但由于您不是程序员,我认为您可能更喜欢更直观的方式,您可以轻松调整自己。

If you can save your Excel spreadsheet as a CSV file then this python script will do what you want.如果您可以将 Excel 电子表格保存为 CSV 文件,那么此 python 脚本将执行您想要的操作。

with open('data.csv') as file:
    data_list = [l.rstrip('\n').split(',') for l in file]

counter = 1

for x in range (1, len (data_list)) :
    output_file_name = str (counter) + '.txt'
    with open (output_file_name, 'w' ) as file :
        for x in range (len (data_list [counter])) :
            print (x)
            output_string = data_list [0] [x] + '=' + data_list [counter] [x] + ';\n'
            file.write (output_string)
    counter += 1

I got this to work in Excel.我让它在 Excel 中工作。 You can expand the length of the variables x,y and z to match your situation and use LastRow , LastColumn methods to find the dimensions of your data set.您可以扩展变量 x、y 和 z 的长度以匹配您的情况,并使用LastRowLastColumn方法来查找数据集的维度。 I named the original worksheet "Data", as shown below.我将原始工作表命名为“数据”,如下所示。

Sub TestExportText()

    Dim Hdr(1 To 4) As String
    Dim x As Long
    Dim y As Long
    Dim z As Long

    For x = 1 To 4
        Hdr(x) = Cells(1, x)
    Next x

    x = 1
    For y = 1 To 5

        ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = y
        For z = 1 To 4
        With ActiveSheet
                .Cells(z, 1) = Hdr(z) & "=" & Sheets("Data").Cells(x + 1, z) & ";"
        End With
        Next z
        x = x + 1
        ActiveSheet.Move
        ActiveWorkbook.ActiveSheet.SaveAs Filename:="File" & y & ".txt", FileFormat:=xlTextWindows
        ActiveWorkbook.Close SaveChanges:=False
    Next y

End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM