Read .txt file

Hello,

I checked github examples and documentations but I did not get the best solution to read .txt and convert the dataframe. I can do it when I read the data from the path (from my personal work that I prepared on PyCharm) But, when I tried to upload the data;

page_2.data_type.file = FileField(‘Upload a file’, file_types=[‘.txt’], max_size=5_000_000)

I cannot read it with my current code block:

…
elif params.page_2.data_type.record == “Multicolumn Acceleration”:

        with open(params.page_2.data_type.file, "r") as file:
            lines = file.readlines()

        # Skip the first line of the file
        lines = lines[params.page_2.data_conf.skipped_line:]

        # Split each row into individual values
        data = []
        for line in lines:
            values = line.split()
            for i in range(0, len(values), params.page_2.data_conf.num_col):
                row = [float(v) for v in values[i:i + params.page_2.data_conf.num_col]]
                data.append(row)

        df = pd.DataFrame(data)

As I understood I cannot use open() function if I try to read upload file without the path. Is this possible to read .txt file with any way? I also checked @ParamsFile but I got and error like “you cannot use this entity etc.”

What is the best solution for that?

Thanks for the help for now.

You can find more about files using the FileField in the docs.

In your case this would look something like this:

file_resource = params.page_2.data_type.file  # obtain the file resource from the field
file = file_resource.file  # get the file (of type `File`) from the file resource
with file.open() as f:  # open the `File` as usual
    lines = f.readlines()

Hi,

Thanks for the answer. Actually I tried whatever I found in the documentation. Now, I tried your solution but I got an error about the readlines() command.

Here is my code block:

    elif params.page_2.data_type.record == "Time - Acceleration":

        file_resource = params.page_2.data_type.file  # obtain the file resource from the field
        file = file_resource.file  # get the file (of type `File`) from the file resource
        with file.open() as f:  # open the `File` as usual
            lines = f.readlines()

            lines = lines[params.page_2.data_conf.skipped_line:]
            data = []
            for line in lines:
                values = line.split()
                if params.page_2.data_conf.acc == 2:
                    data.append([float(values[0]), float(values[1])])
                else:
                    data.append([float(values[1]), float(values[0])])
            df = pd.DataFrame(data, columns=["t", "acc0"])
            dt = df["t"][1] - df["t"][0]

I just uploaded the .txt file but still cannot read it. Am I missing something I did not get it tbh.

You can use f.read() instead, and (if necessary), split the lines yourself.

1 Like