*Image Transcription: Code*
---
def read_csv_file(file_name):
data = []
# Read text file
file = open(file_name, "r")
line = file.readline()
while line:
item = line.replace("]", "").replace("[", "").split(" ")
Time = float(item[0])
Px = float(item[1])
Py = float(item[2])
Pz = float(item[3])
C1 = float(item[4])
C2 = float(item[5])
C3 = float(item[6])
array_point = np.array([Time, Px, Py, Pz, C1, C2, C3])
data.append(array_point)
line = file.readline()
file.close()
return data
---
^^I'm a human volunteer content transcriber and you could be too! [If you'd like more information on what we do and why we do it, click here!](https://www.reddit.com/r/TranscribersOfReddit/wiki/index)
Shorter, and more Pythonic:
def read_csv_file(file_name):
data = []
with open(file_name, 'r') as file:
for line in file:
items = line.replace(']', '').replace('[', '').split(' ')
array_point = np.array(float(x) for x in items)
data.append(array_point)
return data
I haven't had cause to use pandas much, so I don't really know what's in it. And anyway, OP clearly needs to be made aware that text files are iterable by line.
Or better, instead of iterating over each line :
def read_csv_file(file_name):
data = []
with open(file_name, "r") as file:
lines = file.readlines()
for line in lines:
items = line.replace("]", "").replace("[", "").rstrip("\n").split(" ")
array_point = np.array(float(x) for x in items)
data.append(array_point)
return data
def read_csv_file(file_name):
with open(file_name, 'r') as file:
line_items = file.read().replace(']', '').replace('[', '').split('\n')
return [np.array(float(x) for item in li) for li in line_items]
Yes, pandas is actually the way to go. Without the brackets, we would have something like this :
import pandas as pd
df = pd.read_csv(
filename,
columns=["time", "px", "py", "pz", "c1", "c2", "c3"],
sep=" ",
)
A bunch of stuff, but most egregious is the lack of a context for the open file (i.e. using \`with open(path) as file\`) and the uncanny do-while-like construction instead of just \`for line in file\`
Also if you're already using numpy you might as well try np.loadtxt or np.genfromtxt. Lets you set a delimiter for the space and a converter for the \]\[.
... and I was gonna say masking builtin name \`file\` but turns out that's been gone since python3. Well, that's a relief!
Well, sure, but you'll want a context manager in case of an exception. Consider a ValueError raised from the float cast due to bad input data -- the file would remain open, but if you use the \`open\` context manager it will close the file before raising the exception.
* The lack of a context manager to open the file
* Like u/korvbert said, the `do-while-like` construction is not the most clear to understand
* The CSV clearly doesn't contain any column names, which makes it hard to understand the different types of data
* Using `pandas` would have been easier and eventually more efficient than a homemade function
chatGTP code be like
*Image Transcription: Code* --- def read_csv_file(file_name): data = [] # Read text file file = open(file_name, "r") line = file.readline() while line: item = line.replace("]", "").replace("[", "").split(" ") Time = float(item[0]) Px = float(item[1]) Py = float(item[2]) Pz = float(item[3]) C1 = float(item[4]) C2 = float(item[5]) C3 = float(item[6]) array_point = np.array([Time, Px, Py, Pz, C1, C2, C3]) data.append(array_point) line = file.readline() file.close() return data --- ^^I'm a human volunteer content transcriber and you could be too! [If you'd like more information on what we do and why we do it, click here!](https://www.reddit.com/r/TranscribersOfReddit/wiki/index)
Shorter, and more Pythonic: def read_csv_file(file_name): data = [] with open(file_name, 'r') as file: for line in file: items = line.replace(']', '').replace('[', '').split(' ') array_point = np.array(float(x) for x in items) data.append(array_point) return data
I would probably do this with pandas read_csv. You even get to keep the column labels
I haven't had cause to use pandas much, so I don't really know what's in it. And anyway, OP clearly needs to be made aware that text files are iterable by line.
Or better, instead of iterating over each line : def read_csv_file(file_name): data = [] with open(file_name, "r") as file: lines = file.readlines() for line in lines: items = line.replace("]", "").replace("[", "").rstrip("\n").split(" ") array_point = np.array(float(x) for x in items) data.append(array_point) return data
def read_csv_file(file_name): with open(file_name, 'r') as file: line_items = file.read().replace(']', '').replace('[', '').split('\n') return [np.array(float(x) for item in li) for li in line_items]
Isn’t polars recommended lately since it’s faster?
Yes, pandas is actually the way to go. Without the brackets, we would have something like this : import pandas as pd df = pd.read_csv( filename, columns=["time", "px", "py", "pz", "c1", "c2", "c3"], sep=" ", )
What’s bad about this code?
A bunch of stuff, but most egregious is the lack of a context for the open file (i.e. using \`with open(path) as file\`) and the uncanny do-while-like construction instead of just \`for line in file\` Also if you're already using numpy you might as well try np.loadtxt or np.genfromtxt. Lets you set a delimiter for the space and a converter for the \]\[. ... and I was gonna say masking builtin name \`file\` but turns out that's been gone since python3. Well, that's a relief!
the file is explicitly closed, not using context is the least of my worries here
Well, sure, but you'll want a context manager in case of an exception. Consider a ValueError raised from the float cast due to bad input data -- the file would remain open, but if you use the \`open\` context manager it will close the file before raising the exception.
that’s good to know
Does not make good use of the language
CSV with spaces as delimiter is weird for example.
* The lack of a context manager to open the file * Like u/korvbert said, the `do-while-like` construction is not the most clear to understand * The CSV clearly doesn't contain any column names, which makes it hard to understand the different types of data * Using `pandas` would have been easier and eventually more efficient than a homemade function
Love the comment `# Read text file` followed by `file = open()` and `line = file.readline()`
What program did you use to create the image if I may ask?
I'd love to know too.
I found this https://carbon.now.sh/
That's awesome!
That's exactly what I used !