Problem with string and \n - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem with string and \n (/thread-33974.html) |
Problem with string and \n - Falassion - Jun-15-2021 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 RE: Problem with string and \n - Yoriz - Jun-15-2021 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())
RE: Problem with string and \n - Falassion - Jun-15-2021 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? RE: Problem with string and \n - Yoriz - Jun-15-2021 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) if an item from the list is printed it will be a representation of the item.print(list1[0])
RE: Problem with string and \n - Gribouillis - Jun-15-2021 Only actual code can be debugged, not a vague description of what the actual code does. RE: Problem with string and \n - Falassion - Jun-15-2021 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. RE: Problem with string and \n - Falassion - Jun-15-2021 Resolved ! Thanks for advice |