Python Forum

Full Version: Problem with string and \n
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I have a csv file from which I correctly take a string in a list (list1). In the csv file the element is like:

row
row2
row3
row4

When i print the list1 the output is like:

[row \n row2 \n row3 \n row4]

What should i do to print like

row
row2
row3
row4

??

Thanks
The list you have shown is not a valid list, if your list actually looks like this ['row \n', 'row2 \n', 'row3 \n', 'row4'] the following will print each item on a separate line.

list1 = ['row \n', 'row2 \n', 'row3 \n', 'row4']
for item in list1:
    print(item.strip())
Output:
row row2 row3 row4
I explain you what i have done.

Made a string like:

str = text + '\n' + text + '\n' + text + '\n' + text

I saved the str in .csv file.

When i extract it from .csv file and insert it in a list and print it i got

[row1\n row2\n row3\n row4]

full list is: ["row1\n row2\n row3\n row4", "row\n row\n row\n row" ]

instead of:

row1
row2
row3
row4

why?
If the list looks like this
list1 = ["row1 \n row2 \n row3 \n row4", "row \n row \n row \n row" ]
and the list is printed it will be a representation of the list.
print(list1)
Output:
['row1 \n row2 \n row3 \n row4', 'row \n row \n row \n row']
if an item from the list is printed it will be a representation of the item.
print(list1[0])
Output:
row1 row2 row3 row4
Only actual code can be debugged, not a vague description of what the actual code does.
with open(file, 'r', newline='') as csvfile: 
            
    reader = csv.reader(csvfile, delimiter=';')

    for row in reader:

        tabella_risultati_vecchi.append(row)


for row in tabella_risultati_vecchi:

    print(row)
RESULT:

['PISTOLESI MATTEO - PETTITI OMAR\nBACCHINI ALESSANDRO - GUIDO DAVIDE\n2-0\n12-06-2021 15:20\nCampo: 1 - Gara n.25\nMain draw Serie Beach 1 - Milano 12 giugno m 2021 -- Round 1° Turno Vincenti\n21 - 15\n21 - 13\n']
['BACCHINI ALESSANDRO - GUIDO DAVIDE\nBRUNI CRISTIANO - CAROTTI STEFANO\n2-1\n12-06-2021 10:30\nCampo: 3 - Gara n.11\nQualificazioni Serie Beach 1 - Milano 12 giugno m 2021, GIRONE C \n21 - 15\n17 - 21\n15 - 13\n']
['FIORETTA SAMUELE - MAJOR ANTONY\nBACCHINI ALESSANDRO - GUIDO DAVIDE\n2-0\n12-06-2021 09:00\nCampo: 3 - Gara n.3\nQualificazioni Serie Beach 1 - Milano 12 giugno m 2021, GIRONE C \n21 - 14\n21 - 16\n']



CVS FILE

"PISTOLESI MATTEO - PETTITI OMAR
BACCHINI ALESSANDRO - GUIDO DAVIDE
2-0
12-06-2021 15:20
Campo: 1 - Gara n.25
Main draw Serie Beach 1 - Milano 12 giugno m 2021 -- Round 1� Turno Vincenti
21 - 15
21 - 13
"
"BACCHINI ALESSANDRO - GUIDO DAVIDE
BRUNI CRISTIANO - CAROTTI STEFANO
2-1
12-06-2021 10:30
Campo: 3 - Gara n.11
Qualificazioni Serie Beach 1 - Milano 12 giugno m 2021, GIRONE C
21 - 15
17 - 21
15 - 13
"
"FIORETTA SAMUELE - MAJOR ANTONY
BACCHINI ALESSANDRO - GUIDO DAVIDE
2-0
12-06-2021 09:00
Campo: 3 - Gara n.3
Qualificazioni Serie Beach 1 - Milano 12 giugno m 2021, GIRONE C
21 - 14
21 - 16
"


Written with this code:

with open(file,'w', newline='') as csvfile: 

    writer = csv.writer(csvfile, delimiter=';')

    for row in tabella_risultati:
        
        if row.find(giocatore_seguire) != -1:
            
            writer.writerow([row])
I tryed to use print(tabella_risultati_vecchi[0]) but it's the same.
Resolved !

Thanks for advice