Home  >  Q&A  >  body text

Python CSV解析

A,B,两列数据
例如:
若B1,B2列相同(其中B列的数据随机,非升降序)
则在C列呈现A1,A2数据差..

如图:

用VBA比较简单,用Python怎么解答.
之前的问题描述实在糟糕,非常抱歉.

阿神阿神2711 days ago394

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 10:17:23

    If column B is the same, it will be displayed in column C. Didn’t you understand? The standard library has cvs support, wouldn’t it be enough to parse and convert it into python data type?

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:17:23

    The requirements are not very clear. Looking at your picture, if the data in columns A and B are sorted, and there are at most two identical data in column B, then sort out your requirements first

    The original data is in columns A and B, both of which are integers. Column B is arranged from small to large. If column B is the same, column A is also arranged from small to large. If the values ​​in column B are the same, the corresponding values ​​in column A are Subtract the values ​​and insert the result into column C

    First of all, the CSV file can be read line by line. The intervals between different cells in the same line are ,, 行尾有n表示结束. 所以思路是按行读取, 存成一个二元列表, 依次比较, 比较完成后再用join插入, and n and it can be stored in the csv file. The code is as follows:

    
        my_list = [[80, 7], [153, 7], [188, 19], [80, 20]]
        
        for i in range(1,len(my_list)):
            if my_list[i-1][1] == my_list[i][1]:
                my_list[i].append(my_list[i][0] - my_list[i-1][0])
                
        print my_list
        # [[80, 7], [153, 7, 73], [188, 19], [80, 20]]
    

    reply
    0
  • Cancelreply