2
def stringToLek(red):
    lek = {}
    deo = red.strip().split("|")
    lek["ser_br"] = int(deo[0])
    lek["fab_naziv"] = deo[1]
    lek["gen_naziv"] = deo[2]
    lek["kol_leka"] = int(deo[3])
    lek["c_leka"] = float(deo[4])
    return lek
def  lekToString(lek):
    return '|'.join([lek['ser_br'], lek['fab_naziv'], lek['gen_naziv'], lek['c_leka'], lek['kol_leka']])

.................................................................................

TypeError: sequence item 0: expected str instance, int found
Busko
  • 39
  • 1
  • 1
  • 3
  • 1
    possible duplicate of [Python How to print list of list](http://stackoverflow.com/questions/17251935/python-how-to-print-list-of-list) – vaultah Dec 22 '14 at 21:11
  • 1
    `join` method works on (lists of) strings. You're trying to use integers. Convert them first, with `str()`. – 101 Dec 22 '14 at 21:13
  • Would you care to phrase a question, to explain what you are trying to achieve and what fails? Currently, people landing on your question must guess all that by themselves if they want to benefit from the answers. – Eusebius Sep 28 '15 at 15:23

3 Answers3

4

All the arguments to join() must be strings. It chokes at your first one, lek['ser_br'] because that is not a string, but rather an int. lek["kol_leka"] and lek["c_leka"] will have the same problem.

jez
  • 14,867
  • 5
  • 37
  • 64
3
def  lekToString(lek):
return '|'.join([str(lek['ser_br']), str(lek['fab_naziv']), str(lek['gen_naziv']), str(lek['c_leka']), str(lek['kol_leka'])])
1

Perhaps instead you should define a class to implement this behavior?

class Lek:
    def __init__(self, red):
        deo = red.strip().split("|")
        self.ser_br = int(deo[0])
        self.fab_naziv = deo[1]
        self.gen_naziv = deo[2]
        self.kol_leka = int(deo[3])
        self.c_leka = float(deo[4])
        return self

    def __str__(self):
        # The order here is different than in the initializer, I guess it's a bug?
        return '|'.join([
            self.ser_br, self.fab_naziv, self.gen_naziv, self.c_leka, self.kol_leka])


def stringToLek(red):
    return Lek(red)

def  lekToString(lek):
    return str(lek)

The defs outside the class are pretty useless at this point, but I left them in just to show you what they would look like.

This may not be the ideal solution to your problem, but at least shows one refactoring which uses object-oriented programming to encapsulate the logic inside a class. Forcing the rest of your code to only use the methods of the class to interact with it feels like a drag when you first start, but it helps tremendously down the line by reducing the internal coupling between different parts of your code.

You could also solve this by having lek be a subclass of str. There are challenges with that, though. See e.g. https://treyhunner.com/2019/04/why-you-shouldnt-inherit-from-list-and-dict-in-python/ (it discusses dict rather than str, but the issues are similar). In practice, if you are careful, you can inherit from str but you'll have to override __new__ instead of __init__.

class Lek(str):
    def __new__(cls, seq):
        self = super().__new__(cls, seq)
        deo = seq.strip().split("|")
        self.ser_br = int(deo[0])
        self.fab_naziv = deo[1]
        self.gen_naziv = deo[2]
        self.kol_leka = int(deo[3])
        self.c_leka = float(deo[4])
        return self
tripleee
  • 175,061
  • 34
  • 275
  • 318