Nasos Gkimi Nasos Gkimi - 2 months ago 5x
Python Question

Multi Questioning

i read data from txt. 1st line is the row's labels and from 2nd to end the data.

Q1)how i can skip line 1?

the data is in form 34,5 and not 34.5 so i can't use float()
i try str.replace() with not success

Q2) any other idea or suggestion in conversion?

Q3)i use python through QGIS as script. any idea how interrupt the "running" of the script??

the code which i have write as now:

import string

with open('D:/test_data/CLOUDS1.txt') as f:
content = f.readlines()
for line in content:
row = line.split()
for i in X,Y:
print i
for j in Y:
j.replace(',' , '.')
print j

Q4) how i can make a list of point from X,Y??


Question 1

If you want to skip the first line, you have to read it from the buffer. If you don't need the column headings, just don't store the result into a variable:

with open('D:/test_data/CLOUDS1.txt') as f:
    f.readline() # Reads the first line but does not store the result
    content = f.readlines() # Reads the rest of the lines

Question 2 and 4

Assuming your file content looks similar to

x_coord y_coord
23,4 45,6
15,6 24,1
65,2 96,03

When replacing parts of a string, the string is not changed in-place. You have to assign the result of the replace call to a new variable and use it:

points = []
for l in content:
    parts = l.split()
    x = float(parts[0].replace(',', '.'))
    y = float(parts[1].replace(',', '.'))
    points.append([x, y])

Or, in condensed form:

points = [[float(p.replace(',', '.')) for p in l.split()] for l in content]

The result in either case is a 2-dimensional list (i.e. a list of lists) where each element in the outer list is a list with 2 elements representing the x and y coordinates of your points:

[[23.4, 45.6], [15.6, 24.1], [65.2, 96.03]]