0

I have python script that reads a .txt. file and appends items to a two different lists:

//Edit: fragment of my input file: enter image description here

list_data_type = [
  ['void'], ['uint8'], 
  [['uint8'], ['uint32']], ['void'], 
  ['void'], ['void'], ['void']
]

list_arg_parameter = [
  [None], ['f_MbistTestType_u8'], 
  ['f_MbistTestType_u8', 'f_chip_id_u32'], [None], 
  [None], [None], [None]
]

I want to join corresponding items of both list, which I know can be done with zip function, but I want to print the result of both lists in a .csv file, and I want that those two list with 2 elements would be written into same slot and it would look like this:

file_name_1;function_name_1; void (None)
file_name_1;function_name_2; uint8 f_MbistTestType_u8
file_name_1;function_name_3; uint8 f_MbistTestType_u8, uint32 f_chip_id_u32

Also I don't want any "[]" or "''" signs. Right now I tried to convert both lists to a string, strip all unnecessary brackets, convert those strings back to list via this function:

def Convert(string):
li = list(string.split(" "))
return li

And then use simple for loop to make pair of elements:

for elements in zip(list_data_type_strpd, list_arg_parameter_strpd): 
    list_func_arg_combined.append(' '.join(elements))

But the result looks like this:

'void '
'uint8 f_MbistTestType_u8'
'uint8 f_MbistTestType_u8'
'uint32 f_chip_id_32'
'void '
'void '
'void '
'void '

And I would like it, like this:

'void '
'uint8 f_MbistTestType_u8'
'uint8 f_MbistTestType_u8, uint32 f_chip_id_32'
'void '
'void '
'void '
'void '

Maybe I turn those list to string unnecessarily? Maybe there is a easier way to accomplish my goal? Any help, critique or different solutions are welcome.

2
  • can you add the code for reading the file and also ass the sample input Commented Dec 17, 2022 at 2:04
  • I can, but my input file is kinda specific. It's is parsed C file via a python module I found online. This input file is in .txt format, but what it really is a big list with lots of parameters. My goal is to gather only those that matter to me and save it in a .csv format. I'll edit my question. Commented Dec 17, 2022 at 9:33

2 Answers 2

1

Using a nested list comprehension and zip (one level to merge the two lists, one level to zip the sublists, one level to extract the strings from the lowest level of list):

out = [', '.join([' '.join([z[0] if isinstance(z, list) else z for z in y])
                  if y[1] else y[0]
                  for y in (zip(*x))])
       for x in zip(list_data_type, list_arg_parameter)]

Output:

['void',
 'uint8 f_MbistTestType_u8',
 'uint8 f_MbistTestType_u8, uint32 f_chip_id_u32',
 'void',
 'void',
 'void',
 'void']
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, seems that Your solution works ;) . Thank You for that, but what about scalability. WiIl it work in case I would like to join 3 pairs in one place? Or if there would be more than one list with two elements? I'm sorry to ask, but I don;t fully understand structure of this code, would appreciate if You would give a quick explanation to every line. Thanks again!
1

Here's my solution to print the lines according to how you want them:

for data_types, arg_parameters in zip(list_data_type, list_arg_parameter):
    print_line = ""
    for data_type, arg_parameter in zip(data_types, arg_parameters):
        if print_line:
            print_line += ", "
        # You could extract the next line to the `convert` function if you wish to do so
        print_data_type = str(data_type).lstrip("['").rstrip("]'")
        print_line += f"{print_data_type} {arg_parameter}"
    print(print_line)

However, I think there's an error in how list_data_type is initialised. Specifically, the 3rd element in list_data_type should be a list of strings, not a list of lists. Specifically,

# This requires us to have a `hacky` convert function
list_data_type = [
  ['void'], ['uint8'], 
  [['uint8'], ['uint32']], ['void'], 
  ['void'], ['void'], ['void']
]
# The proper one would be a list of strings instead
list_data_type = [
  ['void'], ['uint8'], 
  ['uint8', 'uint32'], ['void'],  # Note the difference in this line
  ['void'], ['void'], ['void']
]

Since you mentioned that you have a python script that reads a txt file and initialises list_data_type, you might want to fix that. Once that is fixed, you can remove the convert function and it will print your data in your output nicely.

1 Comment

There is no error, it is written in my input file like this. Please see picture I just added of my input file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.