我正在处理我在文本文件中获得的数据,并且必须随后对其进行分析。我目前正在使用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
我不需要大部分信息,而现在,我需要的只是顶部的接触角和时间(在底部以'@'为前缀)。目前,我有一个脚本,它提取我需要的信息,并创建另一个文本文件,以方便阅读。使用的代码如下:
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
我正在寻找的是一种在电子表格中很好地格式化数据以便于分析的方法。我希望相同行中的角度,相邻单元格中的角度以及下面单元格中的测量时间,但我不确定最好的方法是什么。
任何拥有更多Python经验的人都可以帮助我吗?
编辑:此处的图片显示了我上面尝试解释的内容。({0}}
EDIT2:@RonRosenfeld在下面发布的解决方案有效,但我仍然希望有一个Python解决方案来解决这个问题,如前所述。由于我以前没有Excel VBA的经验,我宁愿使用我熟悉的东西。
答案 0 :(得分:2)
我只是将原始文件或文件读入Excel,只选择那些以Contact Angle或@ token开头的行。我不确定你需要做多少错误检查。以下假定您将选择多个文件,并且每个文件的格式都与您在原始数据中演示的格式相同。它将输出第1行中的角度,以及第2行中的相应时间。它不会检查正确的格式;或者每个角度都有相应的时间。
如果您只选择一个文件,它也不会测试并会出错。如有必要,可以添加该功能。
修改 已修改为 TAB 或 SPACE 作为分隔符;还添加了代码以清除工作表并自动调整列
如果要选择其他参数,也应该很容易修改。
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 Scripting Runtime的引用的宏。它不使用FileSystemObject,而是使用内置的VBA例程来读取文件。我被告知它会跑得更快,但我自己没有测试过。此外,某些类型的数据可能存在问题,但它们似乎不存在于您的文件中,并且它可以在您的样本上正常运行。
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