大约一年前,我在python中编写了一个小程序,它基本上可以自动完成我的一部分工作(在你们的帮助下提供了很多帮助!)然而,我遇到了一个问题。因为我一直在使程序变得越来越好,我意识到Python不想与excel一起玩得很好,并且(没有让你厌烦细节就足以说xlutils不会复制公式)我需要有更多访问excel for my意图。
所以我开始回到第一个方面用VB(2010 Express,如果它有帮助。)我生命中唯一的编程课程是在它上面,它非常直接所以我决定我会回到为此。不幸的是,我已经忘记了我所学到的很多东西,而且我们从来没有真正在兔子洞里找到这么远。所以,长话短说我想:
1)从.csv中读取数据,结构如下:
41,332.568825,22.221759,-0.489714,eow
42,347.142926,-2.488763,-0.19358,eow
46,414.9969,19.932693,1.306851,r
47,450.626074,21.878299,1.841957,r
48,468.909171,21.362568,1.741944,r
49,506.227269,15.441723,1.40972,r
50,566.199838,17.656284,1.719818,r
51,359.069935,-11.773073,2.443772,l
52,396.321911,-8.711589,1.83507,l
53,423.766684,-4.238343,1.85591,l
2)按列5按字母顺序对数据进行排序 3)然后仅选择第5列中带有“l”的那些,按第2列(升序)对数字进行排序并将它们复制到名为coil.csv的新文件中 4)然后仅选择第5列中具有“r”的那些,按列2(降序)对它们进行数字排序,并将它们复制到SAME文件coil.csv(显然附加在其他文件之后)
在我想要出去的所有喧嚣之后:
51,359.069935,-11.773073,2.443772,l
52,396.321911,-8.711589,1.83507,l
53,423.766684,-4.238343,1.85591,l
50,566.199838,17.656284,1.719818,r
49,506.227269,15.441723,1.40972,r
48,468.909171,21.362568,1.741944,r
47,450.626074,21.878299,1.841957,r
46,414.9969,19.932693,1.306851,r
我意识到这可能是一个非常复杂的问题,如果没有人想要处理所有这些问题,我当然明白了,哈哈。无论如何,一些完整的代码,片段,想法甚至相关链接将非常感激。我一直都在谷歌上搜索,但是找到与此相关的可靠信息比预期的要困难。
P.S。这是我正在谈论的python代码片段(尽管它为左侧和权限创建了两个单独的文件,我并不需要) - 如果它对你有帮助的话。
msgbox(msg="Please locate your survey file in the next window.")
mainfile = fileopenbox(title="Open survey file")
toponame = boolbox(msg="What is the name of the shots I should use for topography? Note: TOPO is used automatically",choices=("Left","Right"))
fieldnames = ["A","B","C","D","E"]
surveyfile = open(mainfile, "r")
left_file = open("left.csv",'wb')
right_file = open("right.csv",'wb')
coil_file = open("coil1.csv","wb")
reader = csv.DictReader(surveyfile, fieldnames=fieldnames, delimiter=",")
left_writer = csv.DictWriter(left_file, fieldnames + ["F"], delimiter=",")
sortedlefts = sorted(reader,key=lambda x:float(x["B"]))
surveyfile.seek(0,0)
right_writer = csv.DictWriter(right_file, fieldnames + ["F"], delimiter=",")
sortedrights = sorted(reader,key=lambda x:float(x["B"]), reverse=True)
coil_writer = csv.DictWriter(coil_file, fieldnames, delimiter=",",extrasaction='ignore')
for row in sortedlefts:
if row["E"] == "l" or row["E"] == "cl+l":
row['F'] = '%s,%s' % (row['B'], row['D'])
left_writer.writerow(row)
coil_writer.writerow(row)
for row in sortedrights:
if row["E"] == "r":
row['F'] = '%s,%s' % (row['B'], row['D'])
right_writer.writerow(row)
coil_writer.writerow(row)
答案 0 :(得分:0)
您拥有的一个选项是从一个类开始来保存字段。这允许您覆盖ToString方法以方便输出。然后,读取每一行并将值分配给类的列表是一件相当简单的事情。在你的情况下,你需要额外的步骤,使2个列表排序下降并组合它们:
Class Fields
Property A As Double = 0
Property B As Double = 0
Property C As Double = 0
Property D As Double = 0
Property E As String = ""
Public Overrides Function ToString() As String
Return Join({A.ToString, B.ToString, C.ToString, D.ToString, E}, ",")
End Function
End Class
Function SortedFields(filename As String) As List(Of Fields)
SortedFields = New List(Of Fields)
Dim test As New List(Of Fields)
Dim sr As New IO.StreamReader(filename)
Using sr As New IO.StreamReader(filename)
Do Until sr.EndOfStream
Dim fieldarray() As String = sr.ReadLine.Split(","c)
If fieldarray.Length = 5 AndAlso Not fieldarray(4)(0) = "e"c Then
If fieldarray(4) = "r" Then
test.Add(New Fields With {.A = Double.Parse(fieldarray(0)), .B = Double.Parse(fieldarray(1)), .C = Double.Parse(fieldarray(2)), .D = Double.Parse(fieldarray(3)), .E = fieldarray(4)})
Else
SortedFields.Add(New Fields With {.A = Double.Parse(fieldarray(0)), .B = Double.Parse(fieldarray(1)), .C = Double.Parse(fieldarray(2)), .D = Double.Parse(fieldarray(3)), .E = fieldarray(4)})
End If
End If
Loop
End Using
SortedFields = SortedFields.OrderBy(Function(x) x.B).Concat(test.OrderByDescending(Function(x) x.B)).ToList
End Function
将数据写入csv文件的一种简单方法是使用IO.File.WriteAllLines方法和List的ConvertAll方法:
IO.File.WriteAllLines(" coil.csv", SortedFields("textfile1.txt").ConvertAll(New Converter(Of Fields, String)(Function(x As Fields) x.ToString)))
你会注意到ToString方法如何很容易实现这一点。
如果该类仅用于此,则可以选择将所有字段设置为字符串。