0

Indeed i have a lot to learn about python but i have a question why replace function is not something what we could use?

i tried using this code

f=open("Te2.csv","w")
s=open("Te.csv").read()
s=s.replace('{','')
s=s.replace('}','')
s=s.replace('(','')
s=s.replace(')','')
s=s.replace("'","")
s=s.replace(":",",")
f.write(s)
f.close()

readFile = open("Te2.csv")
lines = readFile.readlines()
readFile.close()
w = open("Te2.csv",'w')
w.writelines([item for item in lines[:10]])
w.close()

and i came to se same result writen in the new file Te2

Acatari, Acatari, 0.0,
 Acatari, Acis, 183.1984286216621,
 Acatari, Adamclisi, 372.52641231771526,
 Acatari, Adjud, 200.36162156879055,
 Acatari, Afumati, 251.49065927408915,
 Acatari, Agas, 121.63622537704428,
 Acatari, Agigea, 409.27692015889204,
 Acatari, Aiud, 72.63968108608063,
 Acatari, Alba Iulia, 92.8322036095662,
 Acatari, Albac, 127.94211546456722,

First it's not the same output, there are unwanted white space and commas, but there is a deeper reason, we manipulate abstract data, we have a series of records, each record consisting of 2 names and a numerical value, and with this we can read and write csv files. This way of thinking gives better results than unstructured and unmaintanable string manipulations. Let's say there is a good way to do it and a bad way. Writing small helper functions is the good way IMHO.

0

First it's not the same output, there are unwanted white space and commas, but there is a deeper reason, we manipulate abstract data, we have a series of records, each record consisting of 2 names and a numerical value, and with this we can read and write csv files. This way of thinking gives better results than unstructured and unmaintanable string manipulations. Let's say there is a good way to do it and a bad way. Writing small helper functions is the good way IMHO.

Ok .I think i understand what you mean.

I have not used yet conversion between tuples and strings so concerning the first function you said i sould write should i use something like in the next example?

a=[('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.1984286216621), ('Acatari', 'Adamclisi', 372.52641231771526), ('Acatari', 'Adjud', 200.36162156879055), ('Acatari', 'Afumati', 251.49065927408915)]
>>> b=str([('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.1984286216621), ('Acatari', 'Adamclisi', 372.52641231771526), ('Acatari', 'Adjud', 200.36162156879055), ('Acatari', 'Afumati', 251.49065927408915)])
>>> print b
[('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.1984286216621), ('Acatari', 'Adamclisi', 372.52641231771526), ('Acatari', 'Adjud', 200.36162156879055), ('Acatari', 'Afumati', 251.49065927408915)]

Or i should keep on looking for something else?

0

Ok .I think i understand what you mean.

I have not used yet conversion between tuples and strings so concerning the first function you said i sould write should i use something like in the next example?

a=[('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.1984286216621), ('Acatari', 'Adamclisi', 372.52641231771526), ('Acatari', 'Adjud', 200.36162156879055), ('Acatari', 'Afumati', 251.49065927408915)]
>>> b=str([('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.1984286216621), ('Acatari', 'Adamclisi', 372.52641231771526), ('Acatari', 'Adjud', 200.36162156879055), ('Acatari', 'Afumati', 251.49065927408915)])
>>> print b
[('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.1984286216621), ('Acatari', 'Adamclisi', 372.52641231771526), ('Acatari', 'Adjud', 200.36162156879055), ('Acatari', 'Afumati', 251.49065927408915)]

Or i should keep on looking for something else?

No, the first function's argument is a single tuple like ('Acatari', 'Aiud', 72.639). It's return value is a single string with format "Acatari, Aiud, 72.639\n"

Edited by Gribouillis: n/a

0

No, the first function's argument is a single tuple like ('Acatari', 'Aiud', 72.639). It's return value is a single string with format "Acatari, Aiud, 72.639\n"

ok. Then as str() is not what i need and i am quite new in python could you direct me to any kind of tutorial or python resource to help me edit my function?

0

I see there they refer to string formatting in python3.0. and i have 2.7.
What other resource could i use for my version of python?

What they say for python 3 about string formatting applies to 2.7

Edited by Gribouillis: n/a

0

What they say for python 3 about string formatting applies to 2.7

As much as i can try using my knowledge of python i simply can not figure out how to solve the first function.Is there any other way to format that tuple from ('Acatari', 'Aiud', 72.639) to "Acatari, Aiud, 72.639\n"

0

Please show decent effort to learn Python, why not for example go through some simple exercises in DaniWebs beginners threads you can see in beginning of thread listing and do simplest programs yourself. After you gain confidence you can tackle bigger programs.

There are plenty of ways to do things, even in Python where the basic philosophy is to have only way to do things. But nobody can learn Python instead of you. However, with honest efforts of learning, the process can be made easier and faster with others help.

>>> cities_and_distance =  ('Acatari', 'Aiud', 72.639)
>>> cityline =  ', '.join(map(str,cities_and_distance))
>>> cityline += '\n'
>>> cityline
'Acatari, Aiud, 72.639\n'
>>> print cityline
Acatari, Aiud, 72.639
0

Please show decent effort to learn Python, why not for example go through some simple exercises in DaniWebs beginners threads you can see in beginning of thread listing and do simplest programs yourself. After you gain confidence you can tackle bigger programs.

There are plenty of ways to do things, even in Python where the basic philosophy is to have only way to do things. But nobody can learn Python instead of you. However, with honest efforts of learning, the process can be made easier and faster with others help.

>>> cities_and_distance =  ('Acatari', 'Aiud', 72.639)
>>> cityline =  ', '.join(map(str,cities_and_distance))
>>> cityline += '\n'
>>> cityline
'Acatari, Aiud, 72.639\n'
>>> print cityline
Acatari, Aiud, 72.639

I did not wish to be disrespectful and i know that is not an escuse and i will try to search more carefuly before posting next time

0

Using tonyjv'advice i tried the fallowing with the code for the first function

def entry_to_line(entry):
    re= ('Acatari', 'Aiud', 72.639)
    entry= ', '.join(map(str,re))
    return entry_to_line

for the second one i tried to find a way to convert from string to tuples for he first time in shell

tuple ("Acatari, Aiud, 72.639\n")
('A', 'c', 'a', 't', 'a', 'r', 'i', ',', ' ', 'A', 'i', 'u', 'd', ',', ' ', '7', '2', '.', '6', '3', '9', '\n')

But it seems not to be a fit format.

0

Using tonyjv'advice i tried the fallowing with the code for the first function

You are making some big mistake when you making it a fuction.
You are putting in an argument to function that you not use inside the function entry
And you making a recursive call of the function return entry_to_line .

def entry_to_line():
    re = ('Acatari', 'Aiud', 72.639)
    entry = ', '.join(map(str,re))
    return entry

print entry_to_line()

#Or with list comprehension
def entry_to_line():
    re = ('Acatari', 'Aiud', 72.639)
    entry = ', '.join([str(i) for i in re])
    return entry

print entry_to_line()

or the second one i tried to find a way to convert from string to tuples for he first time in shell

No you are not it is a tuple and you are using a tuple.

>>> a = ('Acatari', 'Aiud', 72.639)
>>> type(a)
<type 'tuple'>

>>> b = ', '.join(map(str, ('Acatari', 'Aiud', 72.639)))
>>> type(b)
<type 'str'>
>>> b
'Acatari, Aiud, 72.639'

>>> c = ', '.join([str(i) for i in ('Acatari', 'Aiud', 72.639)])
>>> type(c)
<type 'str'>
>>> c
'Acatari, Aiud, 72.639'
>>>

Edited by snippsat: n/a

0

You are making some big mistake when you making it a fuction.
You are putting in an argument to function that you not use inside the function entry
And you making a recursive call of the function return entry_to_line .

def entry_to_line():
    re = ('Acatari', 'Aiud', 72.639)
    entry = ', '.join(map(str,re))
    return entry

print entry_to_line()

But then how could i use inside the function the argument -entry so that when i call the function it will return the entry not a tuple but a string?

No you are not it is a tuple and you are using a tuple.

>>> a = ('Acatari', 'Aiud', 72.639)
>>> type(a)
<type 'tuple'>

>>> b = ', '.join(map(str, ('Acatari', 'Aiud', 72.639)))
>>> type(b)
<type 'str'>
>>> b
'Acatari, Aiud, 72.639'

>>> c = ', '.join([str(i) for i in ('Acatari', 'Aiud', 72.639)])
>>> type(c)
<type 'str'>
>>> c
'Acatari, Aiud, 72.639'
>>>

Maybe i did not express myself in the right way. I tried to convert thee string resulted from the previos function in a tuple and i thought that using tuple () will do that for me.

0
def entry_to_line():
    re = ('Acatari', 'Aiud', 72.639)
    entry = ', '.join(map(str,re))
    return entry

The explicit tuple ('Acatari', 'Aiud', 72.639) should not appear in your function. Your function should work for any tuple of this form, and should take a single argument entry.

Edited by Gribouillis: n/a

0

The explicit tuple ('Acatari', 'Aiud', 72.639) should not appear in your function. Your function should work for any tuple of this form, and should take a single argument entry.

You mean that it sould be somethong like this?

def entry_to_line(entry)
    entry = ', '.join(map(str,entry))
    return entry

print entry_to_line()

Or i got it again confused?

0

Or i got it again confused?

It`s right and you could use an other name so it`s not entry as an function argument an a variable name.

def entry_to_line(tuple_in):
    entry = ', '.join(map(str,tuple_in))
    return entry

#re = ('Acatari', 'Aiud', 72.639)
test = (1,2,3)
print entry_to_line(test)
print type(entry_to_line(test))
0

You mean that it sould be somethong like this?

def entry_to_line(entry)
    entry = ', '.join(map(str,entry))
    return entry

print entry_to_line()

Or i got it again confused?

It's almost correct. There is a missing newline if we want to adhere to the specifications, also it's better to chose a new name rather than reusing the parameter name

def entry_to_line(entry)
    line = ', '.join(map(str,entry)) + "\n"
    return line

The other solution uses the format() method

def entry_to_line(entry):
    a, b, dist = entry
    line = "{0}, {1}, {2:.3f}\n".format(a, b, dist)
    return line
0

It's almost correct. There is a missing newline if we want to adhere to the specifications, also it's better to chose a new name rather than reusing the parameter name

def entry_to_line(entry)
    line = ', '.join(map(str,entry)) + "\n"
    return line

The other solution uses the format() method

def entry_to_line(entry):
    a, b, dist = entry
    line = "{0}, {1}, {2:.3f}\n".format(a, b, dist)
    return line

But for the conversion of the string to tuple the last function def line_to_entry(line) is there some kind of formating as there was for conversion from tuples to string
line = ', '.join(map(tuple,line)) or there is something that i am missing from the conversion ?

It seems that the formating is not corect as i got this error:
line=', '.join(map(tuple,line))
TypeError: sequence item 0: expected string, tuple found
So what could i use instead?

Edited by toritza: n/a

0

This is the entire code i was referring to

def entry_to_line(entry):
    line = ', '.join(map(str,entry)) + "\n"
    return line

def line_to_entry(lin):
    lin=', '.join(map(tuple,lin))
    return lin

if __name__ ==  "__main__":
    entry = ('Acatari', 'Aiud', 72.639)
    lin = entry_to_line(entry)
    assert lin == "Acatari, Aiud, 72.639\n"
    entry2 = line_to_entry(lin)
    assert entry2 == entry

And this is the error:
File "E:\Users\Toritza\poate.py", line 13, in <module>
entry2 = line_to_entry(lin)
File "E:\Users\Toritza\poate.py", line 6, in line_to_entry
lin=', '.join(map(tuple,lin))
TypeError: sequence item 0: expected string, tuple found

So what seems to be the problem?

0

You are doing some some thing that are not good at all.
assert lin == "Acatari, Aiud, 72.639\n"
Read about the use of assert and that you use == make it strange¨.
assert <<expression, error_message>> is a form of error checking.
It raises an exception if the expression evaluates to false and include the error message.

If you want to convert back to tuple it look like this.
I think you should do some basic reading about python.

def line_to_entry(lin):
    lin = tuple(lin.split(', '))
    return lin

if __name__ ==  "__main__":
    lin = "Acatari, Aiud, 72.639\n"
    print line_to_entry(lin)
0

You are right i have not paid enough attention to the code.
But retourning to my generated matrix, as Gribouillis suggested i had to change the format of my Te.csv file in order to write the way i wanted the matrix

name1    name2     name3
name1    0      distA     distB
name2  DistC     0        DisD
name3  DiistF   DistV      0

and not the way it looks now

{('Berzasca', 'Berzasca'): 0.0,
 ('Berzasca', 'Liubcova'): 5.155523938970983,
 ('Berzasca', 'Moldova Veche'): 27.32911790373754,
 ('Liubcova', 'Berzasca'): 5.155523938970983,
 ('Liubcova', 'Liubcova'): 0.0,
 ('Liubcova', 'Moldova Veche'): 22.194840760286052,
 ('Moldova Veche', 'Berzasca'): 27.32911790373754,
 ('Moldova Veche', 'Liubcova'): 22.194840760286052,
 ('Moldova Veche', 'Moldova Veche'): 0.0}

So what i have problems in understanding this: how could i use these lines of code that you repaired and what changes should i make to the rest of my code write in a file the desired distance matrix.

0

You are right i have not paid enough attention to the code.
But retourning to my generated matrix, as Gribouillis suggested i had to change the format of my Te.csv file in order to write the way i wanted the matrix

name1    name2     name3
name1    0      distA     distB
name2  DistC     0        DisD
name3  DiistF   DistV      0

and not the way it looks now

{('Berzasca', 'Berzasca'): 0.0,
 ('Berzasca', 'Liubcova'): 5.155523938970983,
 ('Berzasca', 'Moldova Veche'): 27.32911790373754,
 ('Liubcova', 'Berzasca'): 5.155523938970983,
 ('Liubcova', 'Liubcova'): 0.0,
 ('Liubcova', 'Moldova Veche'): 22.194840760286052,
 ('Moldova Veche', 'Berzasca'): 27.32911790373754,
 ('Moldova Veche', 'Liubcova'): 22.194840760286052,
 ('Moldova Veche', 'Moldova Veche'): 0.0}

So what i have problems in understanding this: how could i use these lines of code that you repaired and what changes should i make to the rest of my code write in a file the desired distance matrix.

Snippsat's solution is not good because the third tuple item is not a float. The solution is

def line_to_entry(line):
    a, b, dist = line.split(', ')
    return (a, b, float(dist))

I agree with snippsat that you have some very basic python to learn, and since you are reading and writing files, it starts with converting strings to other fundamental data types like tuples, lists and dicts, and back.

Now with our helper functions, this is how we can write the csv file

def write_csv(matrix, filename):
    entries = get_entries(matrix)
    with open(filename, "w") as fout:
        for entry in entries:
            fout.write(entry_to_line(entry))

if __name__ == "__main__":
    matrix = distance_matrix(coords_list)
    write_csv(matrix, "Te.csv")

To read the file, we'll use some code like

with open("Te.csv") as fin:
    for line in fin:
        entry = line_to_entry(line)
        # do something with the entry

Try to write the file, see how it looks, then write a new function which will read a list of 3 entries in the file and return this list.

Edited by Gribouillis: n/a

0

both you are Snippsat are right. I began learnig python doing exercies which involved reading files and writing them using function like sum or average. I will definetely have to get a better look to the basic elements of python.

Concerning the file i succeded to write in Te this format using the provided code.

Acatari, Acatari, 0.0
Acatari, Acis, 183.198428622
Acatari, Adamclisi, 372.526412318
Acatari, Adjud, 200.361621569
Acatari, Afumati, 251.490659274
Acatari, Agas, 121.636225377
Acatari, Agigea, 409.276920159
Acatari, Aiud, 72.6396810861
Acatari, Alba Iulia, 92.8322036096

As for the generation of a new function which will read a list of 3 entries in the file and return this list i will try to make it and post here.
Meanwhile has anyone so far used the DataFrame of module panda in python?

0

This is the function you were talking about?

readFile = open("Te.csv")
lines = readFile.readlines()
readFile.close()
w = open("Tee.csv",'w')
w.writelines([item for item in lines[:3]])
print lines[:3]
w.close()

This is what it writes in the new file

Acatari, Acatari, 0.0
Acatari, Acis, 183.198428622
Acatari, Adamclisi, 372.526412318

This is what it prints

['Acatari, Acatari, 0.0\n', 'Acatari, Acis, 183.198428622\n', 'Acatari, Adamclisi, 372.526412318\n']

Is this what i Should have done ?

0

This is the function you were talking about?

readFile = open("Te.csv")
lines = readFile.readlines()
readFile.close()
w = open("Tee.csv",'w')
w.writelines([item for item in lines[:3]])
print lines[:3]
w.close()

This is what it writes in the new file

Acatari, Acatari, 0.0
Acatari, Acis, 183.198428622
Acatari, Adamclisi, 372.526412318

This is what it prints

['Acatari, Acatari, 0.0\n', 'Acatari, Acis, 183.198428622\n', 'Acatari, Adamclisi, 372.526412318\n']

Is this what i Should have done ?

Once again, you fail miserably, you didn't even write a function and it doesn't return 3 entries but 3 lines. Here is the code

def line_to_entry(line):
    a, b, dist = line.split(', ')
    return (a, b, float(dist))


def read_3_entries():
    result = list()
    with open("Te.csv") as filein:
        for i, line in enumerate(filein):
            if i >= 3:
                break
            entry = line_to_entry(line)
            result.append(entry)
    return result


print read_3_entries()

""" my output -->
[('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.19842862199999), ('Acatari', 'Adamclisi', 372.52641231799998)]
"""

Data structures are very important in your program, our function returns a list of 3 tuples and not 3 strings. Now you should write a function which takes this list as argument and returns the first line of the display that you want. It's another string formatting exercise

"         |  Acatari  |   Acis   |  Adamclisi  |\n"

A question is how are you going to define each column's width ?

About the DataFrame of module panda, it's probably a nice data structure, but you should concentrate on learning python's fundamental data structures instead.

Edited by Gribouillis: n/a

0

I tried this code in shell first:

>>> elements=[('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.198428622), ('Acatari', 'Adamclisi', 372.526412318)]
>>> from operator import itemgetter
>>> map(itemgetter(1), elements)
['Acatari', 'Acis', 'Adamclisi']

in idle i tried this:

import operator
result = operator.itemgetter(1)
tup=('Acatari', 'Acis', 0.0)
print map(result, tup).index("Acatari")

but i got this
print map(result, tupl).index("Acatari")
TypeError: 'float' object is not subscriptable
What might seem to be the problem?

If i am again wrong i appologize.i will be searching another solution.

Edited by toritza: n/a

0

I tried this code in shell first:

>>> elements=[('Acatari', 'Acatari', 0.0), ('Acatari', 'Acis', 183.198428622), ('Acatari', 'Adamclisi', 372.526412318)]
>>> from operator import itemgetter
>>> map(itemgetter(1), elements)
['Acatari', 'Acis', 'Adamclisi']

in idle i tried this:

import operator
result = operator.itemgetter(1)
tup=('Acatari', 'Acis', 0.0)
print map(result, tup).index("Acatari")

but i got this
print map(result, tupl).index("Acis")
TypeError: 'float' object is not subscriptable
What might seem to be the problem?

If i am again wrong i appologize.i will be searching another solution.

map() is not very useful in current versions of python. You can write

cities = [ entry[1] for entry in elements ]

and this yields the list ['Acatari', 'Acis', 'Adamclisi'] . This is not yet our string. There are useful functions that you could use. Try "HELLO".join(cities) for example. Then look at "HELLO".center(15) and also len('Acatari') .

Edited by Gribouillis: n/a

0

I am sorry but i am having troubles in understanding what "HELLO".center(15) and also len('Acatari') are.

0

I am sorry but i am having troubles in understanding what "HELLO".center(15) and also len('Acatari') are.

Here is an exemple in a shell

>>> "HELLO".center(15, "-")
'-----HELLO-----'
>>> len("a")
1
>>> len("ab")
2
>>> len("abc")
3
>>> len("zzz")
3
>>> len("this string has 29 characters")
29
>>> "HELLO".center(20, "$")
'$$$$$$$HELLO$$$$$$$$'
>>> help("HELLO".center) # type q to exit the help

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.