Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with string and \n
#1
Question 
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
Yoriz write Jun-15-2021, 07:25 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
Reply
#3
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?
Reply
#4
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
Reply
#5
Only actual code can be debugged, not a vague description of what the actual code does.
Reply
#6
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.
Gribouillis likes this post
Reply
#7
Resolved !

Thanks for advice
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  f string concatenation problem growSeb 3 499 Jun-28-2021, 05:00 AM
Last Post: buran
  how to deal with problem of converting string to int usthbstar 1 647 Jan-05-2021, 01:33 PM
Last Post: perfringo
  string problem Mathisdlg 6 1,267 Aug-05-2020, 09:31 AM
Last Post: Mathisdlg
  Unicode string index problem luoheng 6 1,230 Nov-23-2019, 03:04 PM
Last Post: luoheng
  simple string & input problem kungshamji 5 1,541 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Problem with inserting a string in to Sqlite db darktitan 3 2,368 Mar-03-2019, 06:30 PM
Last Post: stranac
  IP string manipulation problem TheRealNoob 12 3,292 Feb-04-2019, 09:29 AM
Last Post: perfringo
  problem in replace string into file Saeid_Bibak 1 1,094 Jan-20-2019, 09:46 PM
Last Post: Gribouillis
  String slicing problem Ollew 4 1,498 Sep-08-2018, 08:07 PM
Last Post: Ollew
  Encode string split problem sripylearn 3 1,896 Aug-14-2018, 02:42 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020