Is it possible to use the value printed on the for statement?

Asked 2 years ago, Updated 2 years ago, 70 views

I printed out the i value using the following equation.

path ="./"
file_list = os.listdir(path)
file_list_csv = [file for file in file_list if file.endswith(".csv")]

g = it.groupby(file_list_csv, lambda name:name[:5])

for k, v in v:
   print(k)

   for i in v:
      print(i)

Output result is

The result of print(i) is

2TB-4UDT.csv

2TB-4NST.csv

That's how it's printed out.

Then I'll use the result i (continue the code)

    file1= pd.read_csv(i)

    result = pd.concat([file1], axis=1)

    k0 = k+'.csv'

    result.to_csv(k0, header=True, index=False)

You want to merge the two files "2TB-4UDT.csv" and "2TB-4NST.csv" into one Excel file ("2TB-4.csv").

Can I use the result value i?

If you run that code as it is, only the contents of the last file, "2TB-4NST.csv", are saved.

Thank you for reading this long article!

python for

2022-09-20 14:54

1 Answers

for k, v in v:
    files = [ pd.read_csv(filename) for filename in v ]
    result = pd.concat( files, axis=1 )
    result_file_name = k + ".csv"
    result.to_csv(result_file_name, header=True, index=False)


2022-09-20 14:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.