By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

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

100+
P: 440
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
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

100+
P: 440
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

100+
P: 440
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.