C
C
CreativeStory2018-10-04 09:27:07
Python
CreativeStory, 2018-10-04 09:27:07

Python list to csv, how to write correctly?

I parse the project, I take 2 parameters: Breadcrumbs and the name of the product.
I save in csv. The write data looks like this:

['home', '', 'Semiconductors', 'Analog', 'System Management', 'Voltage Detectors'], ON Semiconductor | NC7SB3157P6X

Breadcrumbs are stored in the first cell, the name is stored in the second. Actually the question is, how to save bread crumbs to bring to the form:
Semiconductors > Analog > System Management > Voltage Detectors

Remove brackets, commas, delimiter, put angle brackets as a separator, remove the first home item.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2018-10-04
@CreativeStory

At the input you have crumbs in the form of a list of strings, apparently.
* remove the first paragraph
* remove empty elements
* format with "angle bracket" delimiter

A
Alexey M, 2018-10-04
@al_mo

To form terms from a list, you can use the list comprehension:

lst = ['home', '', 'Semiconductors', 'Analog', 'System Management', 'Voltage Detectors']
# здесь мы проходим по списку
# добавляем в результирующую строку 
# только если строка не является первым элементом списка и имеет ненулевую длину 
result = ' > '.join([x for x in lst if (len(x) and x != lst[0])])
print(result) # Semiconductors > Analog > System Management > Voltage Detectors

line-by-line reading of csv is possible using the csv module , it is included in the standard library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question