7 Replies - 3340 Views - Last Post: 21 November 2010 - 02:45 PM Rate Topic: -----

#1 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

screen output, int + list

Posted 20 November 2010 - 01:16 PM

Hello guys.
i think this is a very simple question, i just dont code frequently with python.
i am trying to read a line from a file and print it to screen with a number attached before the line .

the code i have and what i think should work is this.

Reader = csv.reader(open('t.csv', 'rb'), delimiter='|')
        i = -1        
        for row in Reader:
            i+=1
            print i + row


on line
print i + row
i am trying to concatenate 'i', a number, with a row from file.

the error i get is this
  File "line.py", line 34, in __init__
    print i + row
TypeError: unsupported operand type(s) for +: 'int' and 'list'



i am thinking even if it does not want to concatenate a number and a list, as good as python is on determining types, it should change i to a list type or something like that,

Is This A Good Question/Topic? 0
  • +

Replies To: screen output, int + list

#2 Guest_c.user*


Reputation:

Re: screen output, int + list

Posted 20 November 2010 - 01:40 PM

print str(i) + row
Was This Post Helpful? 0

#3 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: screen output, int + list

Posted 20 November 2010 - 02:01 PM

Thank you c.user for your suggestion ...

here is the error now.

  File "line.py", line 34, in __init__
    print str(i) + row
TypeError: cannot concatenate 'str' and 'list' objects



so i changed the row to string to like str(row)
now my out put is displayed as follows.

0['xxx', 'xxx', 'xxx']
1['1', '2', '3']



as you can see the numbering worked but the format is different. i know this is because i am reading a csv reader...i will try reading the file just by opening it..i can take ideas if you guys have any

again thank you
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: screen output, int + list

Posted 20 November 2010 - 03:02 PM

Another case of read the manual:

Quote

Each row read from the csv file is returned as a list of strings. No automatic data type conversion is performed.

It's even got a nice example.
Was This Post Helpful? 0
  • +
  • -

#5 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: screen output, int + list

Posted 20 November 2010 - 03:03 PM

To convert an array back to a delimited string you can just use
    delimiter.join(array)



So I think this will do what you want:
    print str(i) + " " + "|".join(row)



Or you could make it perhaps a little more explicit and readable like this:
    print "%d %s" % (i, "|".join(row))


This post has been edited by moopet: 20 November 2010 - 03:05 PM

Was This Post Helpful? 1
  • +
  • -

#6 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: screen output, int + list

Posted 20 November 2010 - 04:32 PM

Thank you for the info guys,
this worked.
reader = csv.reader(open("t.csv", "rb"))
        i = 0        
        for row in reader:
            i+=1             
            print i,'|', '|'.join(row)

gave this on screen
laptop:~$ python line.py
1 | x|x|x
2 | x|x|x



one more question while we are on this:

Is there anyway i can make the the result outputed above into one string? The problem is i want to add the entire resulting text into a QTextEdit (qt). this is the part of my pyqt code thats doing this
Layout = QtGui.QHBoxLayout()
box = QtGui.QTextEdit()

#i want to say something like this bellow.
#but 'data' needs to be the result shown in above result.
box.setText(data)

Layout.addWidget(box)



i tried doing this
reader = csv.reader(open("t.csv", "rb"))
        i = 0        
        for row in reader:
            i+=1             
            box.setText (i,'|', '|'.join(row))


the problem with this is that for each for loop iterations it resets the text that was there before in the QTextEdit window .

so with the above input it will just give line 2 as result.
hope my question makes sense. Basically i can not set multiple strings without erasing whats in the QTextEdit already

Greatly appreciate you guys help. Hopefully you guys do QT

This post has been edited by nacholibre: 20 November 2010 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#7 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: screen output, int + list

Posted 20 November 2010 - 05:09 PM

Well I don't remember much QT, but you can certainly assemble the string first, and use "\n" for newlines:
    s = ''
    for row in reader:
        s += "|".join(row) + "\n"
    box.setText(s) 



HTH
Was This Post Helpful? 0
  • +
  • -

#8 nacholibre  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 19-September 09

Re: screen output, int + list

Posted 21 November 2010 - 02:45 PM

I resolved the problem as follows.
1) ignored the concatenating number idea.
2) added another Qtextedit just for line numbers

file = open('t.csv','r')
data = file.read()   
box.setText(data)


Thank you all for help! DIC rules!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1