473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading a line from text, and separating it into variables/ Structure

So i recently was forced to switch from C to python for Numerical Analysis reasons, and being new to Python/NumPy I was wondering if there was any equivalent of the function fscanf for Python.NumPy or how I would go about reading in a line of data and store the individual "strings" into variables.

I figured my best bet was probably splitting the string into x pieces using split(), but I'm not entirely sure how to assign each individual piece of the string to a corresponding variable.

Also wondering if there is an equivalent of a C structure in Python.

Thank you for reading.
Feb 29 '12 #1

✓ answered by dwblas

If you want to insert the entire line, then it would just be data.append(s) which would give you a list of lists=2 dimensions. The alternative would be to append the subset
data.append([s[0], s[3], s[5]]) --> appends 3 fields only, 1st, 4th, and 6th
Note the [] around the 3 fields which indicates a sub-list which can be interpreted as a second dimension. Unless you have a data set in the gigabytes, it is just as easy to append the entire list and then use whatever part is relevant.

And you can always use a class as a replacement for a C structure but since you know what each position is then it is just as easy to say data[x][y] as some variable name.
Expand|Select|Wrap|Line Numbers
  1. test_data="""USB270.15385-29.63146 270.153847 -29.631455 2.966699e+03 -9.99 1.300391e+03 -9.99 -9.99 A-A-- 6.787463e+01 -9.99 1.555773e+02 -9.99 -9.99 10100 | 0.373 13.554 12.928 12.670 AAA"""
  2. ##fileObj = open(file_name)
  3. bogus_file_obj=test_data.split("|")
  4. data = []
  5. for line in bogus_file_obj:
  6.     s = line.split()
  7.     data.append(s)
  8.  
  9. for rec in data:     ## print the results
  10.     print "-"*30
  11.     for sub_rec in rec:
  12.         print sub_rec 
  13. #
  14. #  print using "array" indexing
  15. print "\n==================================\n"
  16. titles=["Name", "Variance", "Third"]
  17. for x in range(len(data)):
  18.     print "-"*30
  19.     for y in range(len(data[x])):
  20.         ## associate a name with the field location
  21.         if y < len(titles):
  22.             print titles[y],
  23.         print data[x][y]

6 5102
Smygis
126 100+
Guessing wildly here but I think what you are after is best accomplished with a dictionary.

But I'm not sure what you are on about with "assign each individual piece of the string to a corresponding variable.". Or exactly how the data looks like. Or how you want to access it later.

But still a dictionary is a great tool.

Expand|Select|Wrap|Line Numbers
  1. >>> dd = {}
  2. >>> dd["add"] = lambda x, y: x+y
  3. >>> dd["hello"] = "Hello world"
  4. >>> dd
  5. {'add': <function <lambda> at 0x0000000002C2CF98>, 'hello': 'Hello world'}
  6. >>> dd["add"](2,7)
  7. 9
Feb 29 '12 #2
Okay, so the data I am dealing with looks like a set of thousands of what is below:

USB270.15385-29.63146 270.153847 -29.631455 2.966699e+03 -9.99 1.300391e+03 -9.99 -9.99 A-A-- 6.787463e+01 -9.99 1.555773e+02 -9.99 -9.99 10100 | ----- ------ ------ ------ | 0.373 13.554 12.928 12.670 AAA | ----- -------- - -------- - -------- - -------- - | --- ---------- - ---------- - --------- - --------- - --------- - ---------- -

I want to get each segment of the line for example "USB 270.15385-29.63146 270.153847" and store it in a two dimensional array named star[i][0].
And the second segment -29.631455 and store it in the same array star[i][1]... for the first 14 segments.

Using numpy I came accross the function genfromtext() where it allows me to get a string of data and break it up based on delimiters, but I'm not entirely sure how to make that into the two dimensional array I want
Feb 29 '12 #3
bvdet
2,851 Expert Mod 2GB
It appears the data you displayed is on one line which makes sense. You can compile a two dimensional list by iterating on the file object something like:
Expand|Select|Wrap|Line Numbers
  1. fileObj = open(file_name)
  2. data = []
  3. for line in fileObj:
  4.     s = line.split()
  5.     data.append((s[0:2], s[2:14]))
  6. fileObj.close()
Feb 29 '12 #4
I am getting a
Type Error: 'int' object is not subscriptable

Let me see if I get the jist of how the for loop works...
It's going to scan the file line by line,
For each line it will split it into however elements it contains.

I don't really understand the append line part of the program, from what I understand, it inserts the first two parts segments of the string in the first dimension of the array, and the last 13 elements on the other part.

I think what I want to do is this here, but I can't get around the int object error :

Expand|Select|Wrap|Line Numbers
  1. fileObj = open(file_name)
  2. data = []
  3. i=0
  4. for line in fileObj:
  5.     s = line.split()
  6.     for j in range(13)
  7.        data[i].append(s[j:j+1])
  8.     i=i+1
  9. fileObj.close()
  10.  
where it inputs all of the data of the line onto one dimension determined by the int i, which is updated every new line
Feb 29 '12 #5
dwblas
626 Expert 512MB
If you want to insert the entire line, then it would just be data.append(s) which would give you a list of lists=2 dimensions. The alternative would be to append the subset
data.append([s[0], s[3], s[5]]) --> appends 3 fields only, 1st, 4th, and 6th
Note the [] around the 3 fields which indicates a sub-list which can be interpreted as a second dimension. Unless you have a data set in the gigabytes, it is just as easy to append the entire list and then use whatever part is relevant.

And you can always use a class as a replacement for a C structure but since you know what each position is then it is just as easy to say data[x][y] as some variable name.
Expand|Select|Wrap|Line Numbers
  1. test_data="""USB270.15385-29.63146 270.153847 -29.631455 2.966699e+03 -9.99 1.300391e+03 -9.99 -9.99 A-A-- 6.787463e+01 -9.99 1.555773e+02 -9.99 -9.99 10100 | 0.373 13.554 12.928 12.670 AAA"""
  2. ##fileObj = open(file_name)
  3. bogus_file_obj=test_data.split("|")
  4. data = []
  5. for line in bogus_file_obj:
  6.     s = line.split()
  7.     data.append(s)
  8.  
  9. for rec in data:     ## print the results
  10.     print "-"*30
  11.     for sub_rec in rec:
  12.         print sub_rec 
  13. #
  14. #  print using "array" indexing
  15. print "\n==================================\n"
  16. titles=["Name", "Variance", "Third"]
  17. for x in range(len(data)):
  18.     print "-"*30
  19.     for y in range(len(data[x])):
  20.         ## associate a name with the field location
  21.         if y < len(titles):
  22.             print titles[y],
  23.         print data[x][y]
Mar 1 '12 #6
Thank you very much for your help!
Mar 1 '12 #7

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

Similar topics

4
by: Rune Berge | last post by:
Is there a neat way to read the entire contents of a file into a string without having to write a read() loop each time? I would have thought there would be some stream that has a method that lets...
4
by: Paul | last post by:
Hello Everybody, Being new to VBA for Access, I have a question about inputing a text file into access. The text file has 23 lines per file and needs to go into 1 record in a table. The 1st...
2
by: Eyinagho Newton | last post by:
Hiya Everyone, Can anyone explain how postgreSQL reads from a text file into tables already created in PostgreSQL? I am also checking the thread in the Forum just to see if someone has...
8
by: bahoo | last post by:
Hi, I have a text file containing a single line of text, such as 0024 How should I read it into a "list"? I tried this, but the "join" did not work as expected. Any suggestions?
3
by: acacia314 | last post by:
I'm attempting to read a data file into a array of structures. The data comes like this Jane Doe 5 245-44-8899 September 15, 2005 Number of entries in the file is unknown. My segment to...
10
by: bodowpin | last post by:
Hello. I am trying to read a text file that contains 1 2 3 it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all...
3
by: compileMe | last post by:
I'm new to everything in c++. I have a question concerning reading ip addresses from a text file into a char* array. I have googled this question and every answer alludes my issue. Here's...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
3
by: dkwan | last post by:
Hello! i have the following data in a file student_id student_name maths_score english_score physics_score can i use a nested structure to read the file? struct student { int id;...
1
by: PakiB | last post by:
the problem is that ,when i try to display the contents of the structure ,nothing shows can anyone help please.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.