Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
csv行重新排列,同时保留值顺序,python_Python_Csv - Fatal编程技术网

csv行重新排列,同时保留值顺序,python

csv行重新排列,同时保留值顺序,python,python,csv,Python,Csv,我有一个csv文件,如下所示: 要创建如下所示的新csv文件: 我的第一个想法是: r1= [] r2 = [] with open('loadba1.csv', "r") as csv_file: data = csv.reader(csv_file, delimiter=',') f or rows in data: r1.append(rows[0])

我有一个csv文件,如下所示:

要创建如下所示的新csv文件:

我的第一个想法是:

r1= []
r2 = []
with open('loadba1.csv', "r") as csv_file:
       
      data = csv.reader(csv_file, delimiter=',')
             f or rows in data:
               r1.append(rows[0])
               r2.append(rows[1])
r1将给出-TRUE

r2将给出-'L_602356450160818331','wan1'

然后再次循环r2以提取每个值并“以某种方式”合并

我也不能失去价值关系,例如TRUE-wan1-L_602356450160818331


我不确定我应该采取什么方法。请告知。

您可能希望在循环时使用手册
,而不是
来执行

import pandas as pd
df = pd.Dataframe("csv1.csv", index = False)
result = []
Id, LoadBal, Interface = "","",""
for index, row in df.iterrows():
    for col, val in row.iteritems():
        if col == 0 and val:
            LoadBal = val
        elif LoadBal:
            Interface = val
            result.append({"LoadBal": LoadBal, "Interface": Interface,"Id": Id })
            Id, LoadBal, Interface = "","",""
        elif col!=0:
            Id = val
result = pd.DataFrame(result)
result.to_csv("csv2.csv")
with open('loadba1.csv', "r") as csv_file:
    data = csv.reader(csv_file, delimiter=',')
    while True:
        try:
            load_bal, interface = next(data)
        except StopIteration:
            break  # end of file
        try:
            _, the_id = next(data)
        except StopIteration:
            raise ValueError("No ID row for %s" % interface)

        ... # write out (load_bal, interface, the_id)

如果您以文本而不是图像的形式提供数据,这会很有帮助。您如何知道新项目何时开始?它总是两行,还是需要合并第一列为空的所有行?