需要从文本文件到电子表格进行分析的数据

我正在处理我在文本文件中获得的数据,并且必须随后进行分析。 我目前正在使用Excel来完成这项任务。 原始文件如下所示:

Contact Angle (deg) 86.20 Wetting Tension (dy/cm) 4.836 Wetting Tension Left (dy/cm) 39.44 Wetting Tension Right (dy/cm) 39.44 Base Tilt Angle (deg) 0.00 Base (mm) 1.6858 Base Area (mm2) 2.2322 Height (mm) 0.7888 Tip Width (mm) 0.9707 Wetted Tip Width (mm) 0.9581 Sessile Volume (ul) 1.1374 Sessile Surface Area (mm2) 4.1869 Contrast (cts) 245 Sharpness (cts) 161 Black Peak (cts) 10 White Peak (cts) 255 Edge Threshold (cts) 111 Base Left X (mm) 4.138 Base Right X (mm) 5.821 Base Y (mm) 2.980 RMS Fit Error (mm) 3.545E-3 @1600 

我不需要这些信息的大部分,而现在,我需要的只是顶部的接触angular和时间(以'@'作为底部的前缀)。 目前,我有一个脚本提取我需要的信息,并创build另一个文本文件,以方便阅读。 使用的代码如下:

 infile = "in.txt" outfile = "newout.out" measure_time = "" with open(infile) as f, open(outfile, 'w') as f2: for line in f: if line.split(): if line.split()[0] == "Contact": contact_angle = line.split()[-1].strip() f2.write("Contact Angle (deg): " + contact_angle + '\n') if line.split()[0][0] == '@': for i in range(1,5): measure_time += (line.split()[0][i]) f2.write("Measured at: " + measure_time[:2] + ":" + measure_time[2:] + '\n') measure_time = "" else: continue 

我正在寻找的是一种方法,使我的数据很好地格式化在电子表格中,以便于分析。 我想要在同一行中的相邻单元格中的angular度,以及下面单元格中的测量时间,但是我不确定最好的方法是什么。

有更多的Python经验的人能帮助我吗?

编辑:这里的图像显示了我试图解释(不佳)以上。 我正在寻找的样品

编辑2:由@RonRosenfeld公布的解决scheme的工作,但我仍然希望有一个Python解决scheme的这个问题,如前所述。 由于我以前没有使用Excel VBA的经验,我宁愿使用我熟悉的东西。

我只是将原始文件或文件读入Excel,只select那些以Contact Angle或@ token开头的行。 我不知道你需要做多less错误检查。 以下假设您将select多个文件,并且每个文件的格式如您在原始数据中所演示的那样。 它将输出第1行的angular度,第2行的相应时间。它不检查格式是否正确。 或者每个angular度都有相应的时间。

如果你只select一个文件,它也不会testing,并会出现错误。 如有必要,可以添加该function。

编辑: 修改以说明TABSPACE作为分隔符; 还添加了代码来清除工作表和自动填充列

如果要select其他参数,也应该很容易修改。

  Option Explicit 'Set Reference to Microsoft Scripting Runtime Sub GetDataFromTextFiles() Dim FSO As FileSystemObject Dim TS As TextStream Dim F As File Dim sLines As Variant Dim I As Long, J As Long Dim sFilePath Dim S As String Dim vLines() As Variant Dim rExtract As Range 'Hard Coded here but could also use a 'User form to select multiple lines vLines = Array("@", "Contact Angle") Set rExtract = [b3] Cells.Clear [a3] = "Contact Angle (deg)" [a4] = "Measured At" sFilePath = Application.GetOpenFilename("Text Files (*.txt), *.txt", MultiSelect:=True) Set FSO = New FileSystemObject For J = LBound(sFilePath) To UBound(sFilePath) Set TS = FSO.OpenTextFile(sFilePath(J), ForReading) Do Until TS.AtEndOfStream = True S = Trim(Replace(TS.ReadLine, Chr(9), Chr(32))) For I = 0 To UBound(vLines) If InStr(1, S, vLines(I)) = 1 Then Select Case I Case 0 '@ With rExtract(2, 1) .Value = TimeSerial(Int(Mid(S, 2) / 100), Mid(S, 2) Mod 100, 0) .NumberFormat = "hh:mm" End With Case 1 '@ rExtract(1, 1) = Mid(S, InStrRev(S, " ") + 1) 'advance to next column after outputting angle Set rExtract = rExtract(1, 2) End Select End If Next I Loop Next J Cells.EntireColumn.AutoFit End Sub 

这是另一个不需要设置对Microsoft脚本运行时的引用的macros。 它不使用FileSystemObject,而是使用内置的VBA例程来读取文件。 我被告知它会更快运行,但我没有自己testing。 另外,某些types的数据可能存在问题,但是它们似乎并不存在于您的文件中,并且可以在您的示例中正常运行。

 Option Explicit Sub GetDataFromTextFiles() Dim sLines As Variant Dim I As Long, J As Long Dim sFilePath Dim S As String Dim vLines() As Variant Dim rExtract As Range 'Hard Coded here but could also use a 'User form to select multiple lines vLines = Array("@", "Contact Angle") Set rExtract = [b3] Cells.Clear [a3] = "Contact Angle (deg)" [a4] = "Measured At" sFilePath = Application.GetOpenFilename("Text Files (*.txt), *.txt", MultiSelect:=True) For J = LBound(sFilePath) To UBound(sFilePath) Open sFilePath(J) For Input As #1 Do While Not EOF(1) Input #1, S S = Trim(Replace(S, Chr(9), Chr(32))) For I = 0 To UBound(vLines) If InStr(1, S, vLines(I)) = 1 Then Select Case I Case 0 '@ With rExtract(2, 1) .Value = TimeSerial(Int(Mid(S, 2) / 100), Mid(S, 2) Mod 100, 0) .NumberFormat = "hh:mm" End With Case 1 rExtract(1, 1) = Mid(S, InStrRev(S, " ") + 1) 'advance to next column after outputting angle Set rExtract = rExtract(1, 2) End Select End If Next I Loop Close #1 Next J Cells.EntireColumn.AutoFit End Sub