473,387 Members | 1,510 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,387 software developers and data experts.

How can I parse this textfile?

I have looked at literally hundreds of ways to parse but I do not know how to do it myself I just don't understand it really.

I have a word list text file like this

abbreviation
abuser
acceptances
acceptor
acceptors
accesses
accessories
accessory
accident
accidents

and it goes on about 100,000 words.


How could I grab those words and drop them into this table code to put them in my mysql database.


insert into `wordlist`(`word`) values ('this is where the words go');
insert into `wordlist`(`word`) values (word2');
insert into `wordlist`(`word`) values ('word3');

etc...
Jan 2 '10 #1
8 2207
Anyone out there that can help?
Jan 2 '10 #2
Dormilich
8,658 Expert Mod 8TB
Anyone out there that can help?
… and is not on vacation.

for 100,000 words I recommend a Prepared Statement to speed up the writing process. additionally you can insert more than 1 entry per execution:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `wordlist` (`word`) VALUES ('word1'),('word2'),('word3');
to get each word, read the file linewise (and while you are there, execute the Prepared Statement). see fgets()
Jan 2 '10 #3
If you're sure (I'm sure you are) every line contains one word, you can use file() which would make the whole thing possible with just one line of code (I love those):

Expand|Select|Wrap|Line Numbers
  1. $sql = 'INSERT INTO x (word) VALUES (\'' . implode("'), ('", array_map('trim', file())) . "');";
The array_map('trim', [array]) is to clear all the newlines left in by file(). See php.net/file for details.

-- edit
You might want to addslashes() the list too.
Jan 2 '10 #4
Dormilich
8,658 Expert Mod 8TB
I’m not quite sure, if an array of 100,000 members will slow down the script due to the memory usage …
Jan 2 '10 #5
If I cut it down to 10,000?
Jan 2 '10 #6
Dormilich
8,658 Expert Mod 8TB
I don’t know much about such issues. I only know that I would make it so, that I don’t have to deal with it. (which means that in my current state of enlightenment, I’d use Prepared Statements)
Jan 2 '10 #7
If the content is in a text file, you have to somehow get it into PHP and into MySQL, so at the very least all the content will be in the memory. I often have PHP scripts - intentionally - that use > 10 MB memory and they work just fine.
Just make sure you insert at least 2% of your content per query/in one query (so 2.000 words per INSERT query would be fine, 100.000 might be too much for MySQL (too long a query)). This way you need max 50 queries, which is still well manageable.

What you could also do, is make a CSV or XML or XLS from the textfile and import it into MySQL using either PhpMyAdmin or the native MySQL import program. Most secure and fast solution, but wouldn't be automatic, but manual.
Jan 2 '10 #8
Dormilich
8,658 Expert Mod 8TB
go for the manual import.
Jan 2 '10 #9

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

Similar topics

7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
1
by: Larry Rekow | last post by:
I have a report that's created each day as a flat textfile. Because I came from the Access world, I created a macro that imports it with a schema that gives meaningful names to the various...
1
by: Michael Schindler | last post by:
Ich habe es nun erreicht, ein textfile einzulesen das nun zeile für zeile im datagrid angezeigt wird. Nun wäre es toll wenn ich das textfile zum beispiel nach jedem blank in eine spalte...
4
by: Kevin Johnson | last post by:
Hi Using C# and a textfile I need to do two things: 1. Read the contents of the textfile and return the number of words contained in it. 2. Output a list of unqiue words (i.e. not...
2
by: kaede | last post by:
Hi all, Given the following textfile (i.e test.txt) 5 5 5 2 E aeiou 4 3 Y abcdefghij
1
by: kachokaratz | last post by:
well i have a problem in my project..since the contents of my textfile is too long, i was wondering how to output the textfile in such a way that you can divide the contents to be able to output...
0
by: ghostface | last post by:
**How do I parse a textfile and edit only a certain part of it. Specifically, just the last column. My textfile looks like this. #Server Group 1...
0
by: Lie Ryan | last post by:
On Wed, 01 Oct 2008 14:09:09 +0200, Tino Wildenhain wrote: There is no need to use eval on that, you could just use: f = open('file.conf') conf = {} for line in f: key, value =...
1
by: asedt | last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile. My problem is that i don't know how to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...

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.