473,748 Members | 10,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fixed-length text file to database script

Hi Guys,

I'm new to Python (mostly) and I'm wanting to use it for a new project
I'm faced with.

I have a machine (PLC) that is dumping its test results into a fixed-
length text file. I need to pull this data into a database (MySQL
most likely) so that I can access it with Crystal Reports to create
daily reports for my engineers.

I've been reading the Python manual for about a week now and I'm
learning a lot. Unfortunately, I was given a deadline today that I
cannot meet without a little help.

I need to know how to write a script that will DAILY pull this text
file into a MySQL database.

Can anyone show me how to do this?

Thanks

Stacey
Aug 13 '08 #1
6 4448
ss******@gmail. com wrote:
Hi Guys,

I'm new to Python (mostly) and I'm wanting to use it for a new project
I'm faced with.

I have a machine (PLC) that is dumping its test results into a fixed-
length text file. I need to pull this data into a database (MySQL
most likely) so that I can access it with Crystal Reports to create
daily reports for my engineers.

I've been reading the Python manual for about a week now and I'm
learning a lot. Unfortunately, I was given a deadline today that I
cannot meet without a little help.

I need to know how to write a script that will DAILY pull this text
file into a MySQL database.

Can anyone show me how to do this?

Thanks

Stacey
Just use the built in import SQL statement to import the information. You don't
really need a Python script. import can handle fixed field records (as well as
CSV, etc.).

-Larry
Aug 13 '08 #2
Larry Bates wrote:
ss******@gmail. com wrote:
>I have a machine (PLC) that is dumping its test results into a fixed-
length text file. I need to pull this data into a database (MySQL
most likely) so that I can access it with Crystal Reports to create
daily reports for my engineers.
[..]
I need to know how to write a script that will DAILY pull this text
file into a MySQL database.

Just use the built in import SQL statement to import the information.
You don't really need a Python script. import can handle fixed field
records (as well as CSV, etc.).
If the input data has to be pre-processed before storing it into the
database a Python script would be needed.

Just in case somebody needs a module for reading fixed-length files in
the spirit of module csv:

http://www.stroeder.com/pylib/fixedlenfields.py

For the MySQL part:
http://mysql-python.sourceforge.net/

Ciao, Michael.
Aug 14 '08 #3
Michael Ströder wrote:
Larry Bates wrote:
>ss******@gmail. com wrote:
>>I have a machine (PLC) that is dumping its test results into a fixed-
length text file. I need to pull this data into a database (MySQL
most likely) so that I can access it with Crystal Reports to create
daily reports for my engineers.
[..]
I need to know how to write a script that will DAILY pull this text
file into a MySQL database.

Just use the built in import SQL statement to import the information.
You don't really need a Python script. import can handle fixed field
records (as well as CSV, etc.).

If the input data has to be pre-processed before storing it into the
database a Python script would be needed.

Just in case somebody needs a module for reading fixed-length files in
the spirit of module csv:

http://www.stroeder.com/pylib/fixedlenfields.py

For the MySQL part:
http://mysql-python.sourceforge.net/

Ciao, Michael.
While you are correct, that is not what the OP asked. There is no reference to
processing data prior to insertion into MySQL database. Also the OP said they
had a 1 day deadline.

-Larry
Aug 14 '08 #4
I have a machine (PLC) that is dumping its test results into a fixed-
length text file.
While it has nothing to do with python, I found that creating a MySQL
table with the proper fixed length char() fields and using 'load data
infile' was the easiest way to deal with that sort of scenario. The
python script is the secondary part, that handles the normalization
and proper typing of the first table to the second, permanent storage
area. But in this case, the more advanced bits are the database and
SQL details, and python is just a very convenient way to build the SQL
statements and execute them.

I'm really not sure what the best way to deal with fixed length data
is in python. I might define a list with the field lengths and use a
string slicing to get the items.. as a first thought:

myfile = '/somewhere/somefile.txt'
sizes = [16,4,8,8,8]

fd = open(myfile,r)

for line in fd.readlines() :

idx1 = 0
for l in sizes :
Aug 14 '08 #5
Sorry, didn't get to finish my script. Have to figure out the deal
with gmail and the tab key someday.

myfile = '/somewhere/somefile.txt'
sizes = [16,4,8,8,8]

fd = open(myfile,r)

data = []
for line in fd.readlines() :
a = []
idx1 = 0
for l in sizes :
idx2 = idx1 + l
a.append(line[idx1:idx2])
idx1 += l
data.append(a)

fd.close()
print data

This isn't tested, and there are probably more elegant ways to do it,
but for quick and dirty I think it should work.
Aug 14 '08 #6
Larry Bates wrote:
While you are correct, that is not what the OP asked. There is no
reference to processing data prior to insertion into MySQL database.
Also the OP said they had a 1 day deadline.
Larry, having a bad day?

I'm confident that the OP is able to sort out *himself* what he needs.
Also the 1 day deadline would not be an obstacle. Would it for you?

Ciao, Michael.
Aug 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
17709
by: Paul Trautwein | last post by:
I'm trying to get an image to float in a window despite scrolling. I've gotten it to work on my Mac using IE 5.2, Netscape, and Safari, but it goes wonky when I test it on a PC. (testing with IE only at the moment.) Positioning is wrong, and it doesn't float at all. Here's a test page: http://www.bdiusa.com/mirrors/test.html I've tested the code using the CSS Validator on the W3 site - and it said it's okay.
6
5407
by: Mason A. Clark | last post by:
Masters: On two or three-column layouts, one column often has a list of links. Scrolling the page hides them. I'm aware there's supposed to be the ability to fix the column (frame-like). I have some bits of such code but haven't yet made it work well. Question: Why have I never seen an example on the web? Not that I've seen everything, but I've seen numerous pages
5
3460
by: Ted Mayett | last post by:
I've read a lot through the google archives, but I cannot seem to find an example... Has someone successfully made css code that allows for position: fixed to work in IE 6.0.2800? What is the url please? My partner is looking to make the top-right graphic stay in view as the reader scrolls down the page: <http://www.solitarytrees.net/trees.htm>
2
5007
by: hq105862 | last post by:
Hi, Is it possible to simulate frames behaviour with CSS? Currently, I've used CSS to create the visual look of my old framed site. For this I've used styles that place the images at absolute positions. Then I thought, if I fixed the image positions, the images will stay put and the 'body' will scroll up, like in frames.
2
4216
by: Jonathan Carmichael | last post by:
I'm trying to create a fixed, no-repeat, centered background image using an external css. Everything works great until I add background-attachment: fixed then it just doesn't show the image at all. I've tried it using the "all in one" {background: url(SGsmall.jpg) no-repeat center fixed}
9
4190
by: pout | last post by:
What are the purposes of fixed-point? When should it be used? I read: #define Int2Fixed(x) (((long)(short)x) << 16) and the fixed-point in 16.16 format. Does the 16 in the MACRO refer to integer or decimal part? For example, if in 8.24, should the macro be: #define Int2Fixed(x) (((long)(short)x) << 24)?
3
2754
by: Chris Dunaway | last post by:
I was using .Net Reflector to look at some methods in the String class and found this one: Friend Sub AppendInPlace(ByVal value As Char*, ByVal count As Integer, ByVal currentLength As Integer) Dim local1 As Char* Fixed local1 = AddressOf Me.m_firstChar Dim num1 As Integer For num1 = 0 To count - 1 local1((currentLength + num1)) = value(num1)
4
11174
by: Otie | last post by:
Hello, I am using the MSFlexGrd Control in VB5. I have 1 fixed row and one fixed column. I am trying to do a sort when the user clicks a column in the FIXED ROW. But when I capture the row number in the click event I get row = 1 if I click on the FIXED row OR the actual row 1. How can I get the grid control to tell me when the user has clicked on the FIXED row and not on row 1 (since they both produce row = 1 and I cannot tell them apart...
2
9763
by: Eric Lindsay | last post by:
Googling suggests that IE7 may support position: fixed; I think this might be handy for some pages I want to do. Does anyone have any comments about whether fixed should be considered for use on new web pages? -- http://www.ericlindsay.com
8
2288
by: ms news group | last post by:
What happens if exception is thown within a fixed block? Will the pinned memory buffer get unpinned? and if the pinning pointer points to a managed memery buffer allocated within the throwing function, will the memory buffer get garbage-collected finally? I check the language spec, also searched the internet, found nothing regarding this question.
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.