467,870 Members | 1,377 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,870 developers. It's quick & easy.

Code refactoring: Reading string data from file and converting the data type

256MB
Hi ,

When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ).

Please suggest me which is the best way of handling the file data.

I- Method:
----------------

Store as single line string data's(upto end of file ) in a list and make use of this string list for the entire application.

When we required that single line data, which is having different data types,we convert it and get that details and make use of these data types in our application when and as required

In a nut shell,while reading the data we are storing as string data,when it is required in the application to do geometric operation ,we convert the data into int /float from the string at that location

II- Method:
---------------

Traverse the singleline data and convert the string data into the respective datatypes and store in a variable (say list).

In a nut shell,while reading the data itself ,we are converting the respective data types and storing it,instead of keeping the data in string format


Store as single line string data for the entire application and whenever you required that single line data,in different data types,you convert it and get the deatils and make use of these data types in your application.

Thanks
PSB
Apr 3 '07 #1
  • viewed: 2883
Share:
3 Replies
bvdet
Expert Mod 2GB
Hi ,

When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ).

Please suggest me which is the best way of handling the file data.

I- Method:
----------------

Store as single line string data's(upto end of file ) in a list and make use of this string list for the entire application.

When we required that single line data, which is having different data types,we convert it and get that details and make use of these data types in our application when and as required

In a nut shell,while reading the data we are storing as string data,when it is required in the application to do geometric operation ,we convert the data into int /float from the string at that location

II- Method:
---------------

Traverse the singleline data and convert the string data into the respective datatypes and store in a variable (say list).

In a nut shell,while reading the data itself ,we are converting the respective data types and storing it,instead of keeping the data in string format


Store as single line string data for the entire application and whenever you required that single line data,in different data types,you convert it and get the deatils and make use of these data types in your application.

Thanks
PSB
I would create a dictionary, set or list from the file data with the values converted. That is not required, but it is my preference. I want to do the work necessary on import to make the data readily available.
Apr 3 '07 #2
256MB
I would create a dictionary, set or list from the file data with the values converted. That is not required, but it is my preference. I want to do the work necessary on import to make the data readily available.

Most of the experienced developers follow the same approach ,and I hope it is a best practice also.

But performance wise which is best way of doing.

Prepare the data in the begining itself by converting ,so that it will be easy for us to do any data manipulation.And no need to think in the middle of the application development ,whether the data is string or int or etc. we will know that our data has converted in the correct format.

Otherwise we will get confused when we get different results,and we have to check for each value,whether it is string or other datatype,with "print" out statements.

Once I faced the problem .when I am passing the ID as "int" to the method.Thinking that I have converted the ID from 'string' to 'int'.When i was struggling for longer time, and after anlayizing the data I came to know that one data type is in "string" format and the input data what i am passing is "int" format


Thanks
PSB
Apr 3 '07 #3
256MB
But performance wise which is the best way of doing.??

Can anybody suggest on this

-PSB
Apr 3 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by David Scemama | last post: by
135 posts views Thread by Xah Lee | last post: by
82 posts views Thread by Edward Elliott | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.