我有一个excel文件,该文件从第三方自动生成的各种txt文件中导入价格。我用其中一个txt文件打了一个路障,因为它使用空格分隔,而其他人使用制表符。由于空间有限,当我分割数据时,我在每一列中得到不同的值。
这是我到目前为止可以打开并阅读文本文件的地方
board[i][j]
这是txt文件的样子:
usFileName = PathName & "\" & "Prices.txt"
If fs.FileExists(usFileName) Then
Set US = fs.OpenTextFile(usFileName, 1)
theData = US.ReadLine
getDate = Split(theData, Chr(0))
curDate = Trim(Left(getDate(0), 10))
If curDate = ActiveSheet.Range("Sheet_Date") Then
Do While Not US.AtEndOfStream
On Error Resume Next
Ln = US.ReadLine
Cols = Split(Ln, " ")
Price = Trim(Cols(7))
NameTrim = Trim(Replace(Cols(1), "USO-", ""))
CellName = Replace(NameTrim, "-", "_") & "_" & Trim(Cols(2))
If ActiveSheet.Range(CellName) Is Nothing Then
''Do Nothing here
On Error Resume Next
Else
Set TxtRng = ActiveSheet.Range(CellName)
If TxtRng = ActiveSheet.Range(CellName) Then
TxtRng.Value = Price
End If
End If
Loop
Else
MsgBox ("The current sheet date does not match the US file import date.")
End If
US.Close
Else
MsgBox ("The file Prices.txt does not exist.")
End If
这就是Cols变量的样子。
答案 0 :(得分:1)
尝试
import seaborn as sns, numpy as np
sns.set(); np.random.seed(0)
x = np.random.randn(100)
ax = sns.distplot(x)
答案 1 :(得分:0)
您可以尝试删除空格并以这种方式替换它们:
Ln = US.ReadLine
Do While (InStr(Ln, " ") > 0)
Ln = Replace(Ln, " ", " ")
Loop
'The only problem I see is the case where you have Time (6:00 PM) this
' would replace the space with Tab. in that case, I would do the following:
Ln = Replace(Ln, " PM", "PM")
Ln = Replace(Ln, " AM", "AM")
Ln = Replace(Ln, " ", vbTab)
'And then put them back
Ln = Replace(Ln, "PM", " PM")
Ln = Replace(Ln, "AM", " AM")
'Finally, split the columns
Cols = Split(Ln, vbTab)
如果要尝试使用固定宽度,请执行以下操作(在Ln = US.Readline之后):
'01/11/2019 06:00 PM USO-FOX-USO E10 8.9929 0.0000
'Do the following only if (InStr(Ln, " ") > 0)
strCol1 = Mid(Ln, 1, 21)
strCol2 = Mid(Ln, 22, 13)
strCol3 = Mid(Ln, 35, 13)
strCol4 = Mid(Ln, 48, 11)
strCol5 = Mid(Ln, 59, Len(Ln))