我怎样才能将每个细胞转移到不同的形式,然后将它们分成 2 个细胞? CSV python

How could I transfer each cell to different form then separate them to 2 cells ? CSV python

我有一个 number.csv 文件,每个单元格包含一个数字。例如,它看起来像这样..

    0,1,2 
   -1,0,1
    1,2,-1

然后我想根据我的规则将每个数字转换为某个名称。

   # The rule is: 
   # 0 for AA
   # 1 for AB
   # 2 for BB
   # -1 for 00 (that's double zero)

因此我会得到类似...

   AA,AB,BB
   00,AA,AB
   AB,BB,00

然后我必须将每个单元格分成两个单元格,然后写入一个新的result.csv文件。最后它看起来像....

   A,A,A,B,B,B
   0,0,A,A,A,B
   A,B,B,B,0,0  

我只知道如何将每个数字替换为某个名称(例如 0 到 AA),然后我坚持如何将每个单元格的 AA 划分为 A,A 并将每一行分隔到下一行。

有谁知道如何通过 csv 模块解决这个问题,或者更聪明的方法来实现这个问题?

非常感谢,

with open(r'G:\number.csv', 'r') as f:  # read
    content = f.read()
    with open(r'G:\number-post.csv', 'wb') as fh: # write
        fh.write(content.replace('-1','O,O').replace('0','A,A').replace('1','A,B').replace('2','B,B').replace('O','0'))

只需将上面的文件名替换为您对应的文件名即可。

使用VBA

Sub user3058889()
    Dim d1 As String, s2 As String
    s1 = "C:\TestFolder\input.csv"
    s2 = "C:\TestFolder\output.csv"
    Close #1
    Close #2
    Open s1 For Input As #1
    Open s2 For Output As #2
    Do While Not EOF(1)
            Line Input #1, textline
            textline = Replace(textline, "-1", "z")
            textline = Replace(textline, "0", "A,A")
            textline = Replace(textline, "1", "A,B")
            textline = Replace(textline, "2", "B,B")
            textline = Replace(textline, "z", "0,0")

            Print #2, textline
    Loop

    Close #1
    Close #2
End Sub