Author Topic: Changes to Text File  (Read 6937 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline IronMan

  • Newbie
  • Posts: 38
    • View Profile
Changes to Text File
« on: March 15, 2020, 02:09:01 am »
Could someone please help me. I am needing a current text file that has my 7 day weather forecast to be put in a little more user friendly, more readable format. Needs the "." removed at the beginning of each day and a blank line between the days.

>>> Here is what the text file looks like now: <<<

.REST OF TONIGHT...A slight chance of showers late in the
evening, then a chance of showers in the late evening and early
morning. Showers likely and a slight chance of thunderstorms
before sunrise. Cloudy. Lows in the mid 50s. Southeast winds
around 5 mph becoming north after midnight. Chance of rain
60 percent.
.SUNDAY...Showers likely and a slight chance of thunderstorms in
the morning, then a slight chance of showers and thunderstorms in
the afternoon. Cloudy. Highs in the lower 60s. North winds 5 to
10 mph. Chance of rain 60 percent.
.SUNDAY NIGHT...Mostly cloudy. Lows around 50. Northeast winds
5 to 10 mph.
.MONDAY...A slight chance of showers in the morning, then a
chance of showers in the afternoon. Cloudy. Highs around 70. East
winds around 5 mph. Chance of rain 30 percent.
.MONDAY NIGHT...Cloudy with a 40 percent chance of showers. Lows
in the upper 50s. East winds around 5 mph.
.TUESDAY...A chance of showers in the morning, then showers
likely and a slight chance of thunderstorms in the afternoon.
Mostly cloudy. Highs in the mid 70s. Chance of rain 60 percent.
.TUESDAY NIGHT...A chance of showers and a slight chance of
thunderstorms in the evening, then a chance of showers after
midnight. Mostly cloudy. Lows in the upper 50s. Chance of rain
40 percent.
.WEDNESDAY...A chance of showers and a slight chance of
thunderstorms in the morning, then showers likely and a slight
chance of thunderstorms in the afternoon. Mostly cloudy. Highs in
the upper 70s. Chance of rain 60 percent.
.WEDNESDAY NIGHT...Showers likely and a slight chance of
thunderstorms. Lows in the mid 60s. Chance of rain 70 percent.
.THURSDAY...Showers likely and a chance of thunderstorms. Highs
around 80. Chance of rain 70 percent.
.THURSDAY NIGHT...A chance of showers and thunderstorms in the
evening, then showers and thunderstorms likely after midnight.
Mostly cloudy. Lows in the upper 60s. Chance of rain 70 percent.
.FRIDAY...Showers and a chance of thunderstorms in the morning,
then showers likely and a chance of thunderstorms in the
afternoon. Highs in the upper 70s. Chance of rain 80 percent.
.FRIDAY NIGHT...Cooler. Showers likely and a slight chance of
thunderstorms in the evening, then a chance of showers after
midnight. Mostly cloudy. Lows in the lower 50s. Chance of rain
70 percent.
.SATURDAY...Cooler. Mostly sunny with a 20 percent chance of
showers. Highs in the mid 60s.



>>> I would like for it to look more like this:<<<

REST OF TONIGHT...A slight chance of showers late in the
evening, then a chance of showers in the late evening and early
morning. Showers likely and a slight chance of thunderstorms
before sunrise. Cloudy. Lows in the mid 50s. Southeast winds
around 5 mph becoming north after midnight. Chance of rain
60 percent.

SUNDAY...Showers likely and a slight chance of thunderstorms in
the morning, then a slight chance of showers and thunderstorms in
the afternoon. Cloudy. Highs in the lower 60s. North winds 5 to
10 mph. Chance of rain 60 percent.
.SUNDAY NIGHT...Mostly cloudy. Lows around 50. Northeast winds
5 to 10 mph.

MONDAY...A slight chance of showers in the morning, then a
chance of showers in the afternoon. Cloudy. Highs around 70. East
winds around 5 mph. Chance of rain 30 percent.

MONDAY NIGHT...Cloudy with a 40 percent chance of showers. Lows
in the upper 50s. East winds around 5 mph.

TUESDAY...A chance of showers in the morning, then showers
likely and a slight chance of thunderstorms in the afternoon.
Mostly cloudy. Highs in the mid 70s. Chance of rain 60 percent.

TUESDAY NIGHT...A chance of showers and a slight chance of
thunderstorms in the evening, then a chance of showers after
midnight. Mostly cloudy. Lows in the upper 50s. Chance of rain
40 percent.

WEDNESDAY...A chance of showers and a slight chance of
thunderstorms in the morning, then showers likely and a slight
chance of thunderstorms in the afternoon. Mostly cloudy. Highs in
the upper 70s. Chance of rain 60 percent.

WEDNESDAY NIGHT...Showers likely and a slight chance of
thunderstorms. Lows in the mid 60s. Chance of rain 70 percent.
.THURSDAY...Showers likely and a chance of thunderstorms. Highs
around 80. Chance of rain 70 percent.

THURSDAY NIGHT...A chance of showers and thunderstorms in the
evening, then showers and thunderstorms likely after midnight.
Mostly cloudy. Lows in the upper 60s. Chance of rain 70 percent.

FRIDAY...Showers and a chance of thunderstorms in the morning,
then showers likely and a chance of thunderstorms in the
afternoon. Highs in the upper 70s. Chance of rain 80 percent.

FRIDAY NIGHT...Cooler. Showers likely and a slight chance of
thunderstorms in the evening, then a chance of showers after
midnight. Mostly cloudy. Lows in the lower 50s. Chance of rain
70 percent.

SATURDAY...Cooler. Mostly sunny with a 20 percent chance of
showers. Highs in the mid 60s.


Any help is appreciated.

Thanks.
Kent

« Last Edit: March 15, 2020, 02:12:42 am by IronMan »

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Changes to Text File
« Reply #1 on: March 15, 2020, 03:06:53 am »
Do you have any code to share showing what you have attempted to do so far?

Welcome to the forum as well :-)
In order to understand recursion, one must first understand recursion.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Changes to Text File
« Reply #2 on: March 15, 2020, 03:30:09 am »
IF LEFT$(text$, 1) = "." THEN
    PRINT CHR$(13); CHR$(13)
.   PRINT MID$(text$, 2); " ";
ELSE
.  PRINT text$;
END IF


It looks like you'd want to do something similar to the above after you read in each line (text$).
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Changes to Text File
« Reply #3 on: March 15, 2020, 07:23:23 am »
It would appear that Steve (McNeill) has carried over beginning lines with a dot into code (lines 3 & 5)!  As Terry says, it would be good to see Kent's efforts so far.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Changes to Text File
« Reply #4 on: March 15, 2020, 07:39:37 am »
It would appear that Steve (McNeill) has carried over beginning lines with a dot into code (lines 3 & 5)!  As Terry says, it would be good to see Kent's efforts so far.

Stupid auto-format wants to put a period before 2 spaces...  I thought I'd disabled that.  /sigh at "helpful technology" and programming.  :P
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline IronMan

  • Newbie
  • Posts: 38
    • View Profile
Re: Changes to Text File
« Reply #5 on: March 15, 2020, 11:22:06 am »
I'm using QB64 which to run a SHELL command that executes CURL from the command prompt which retrieves a text file from the NOAA Weather site.

' Getting NOAA 7 day weather text forecast
SHELL "curl https://tgftp.nws.noaa.gov/data/forecasts/zone/ms/msz024.txt >forecast.txt"

It saves it to my hard drive and is called forecast.txt. Just need a small QB64 application to remove the "." from the beginning of each day and put a space (carriage return) between each day so that it would make it less jumbled and more user friendly readable file.

I'm attaching the forecast.txt file for reference if anyone wants to take a look.

Any help is appreciated.

Thank you.
Kent

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Changes to Text File
« Reply #6 on: March 15, 2020, 11:27:34 am »
Well @IronMan, do you know how to open a file and read it's contents with QB64?

This kinda sounds like homework.
« Last Edit: March 15, 2020, 11:42:53 am by bplus »

Offline IronMan

  • Newbie
  • Posts: 38
    • View Profile
Re: Changes to Text File
« Reply #7 on: March 15, 2020, 12:38:11 pm »
Yea, I know how to open and read the forecast.txt file.

DIM LineTxt AS STRING
CLS
OPEN "forecast.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, LineTxt
PRINT LineTxt
WEND
CLOSE #1


I just need to be able to remove the "." that are at the start of each day, and to put a blank line in between the days and wrote these changes back to the forecast.txt file.

I will continue to do my homework, but please share any ideas.

Thanks
Kent
« Last Edit: March 15, 2020, 12:39:21 pm by IronMan »

Marked as best answer by IronMan on March 15, 2020, 09:53:25 am

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Changes to Text File
« Reply #8 on: March 15, 2020, 12:59:07 pm »
Yea, I know how to open and read the forecast.txt file.

DIM LineTxt AS STRING
CLS
OPEN "forecast.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, LineTxt
PRINT LineTxt
WEND
CLOSE #1


I just need to be able to remove the "." that are at the start of each day, and to put a blank line in between the days and wrote these changes back to the forecast.txt file.

I will continue to do my homework, but please share any ideas.

Thanks
Kent

Reply Number 2.  Replace text$ with LineTxt.  Remove the 2 idiotic auto- periods my iPad inserted.  Put in place of your PRINT line.  Compile and done.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Changes to Text File
« Reply #9 on: March 15, 2020, 01:11:46 pm »
Hi @IronMan,

I strongly suggest you use:
Code: QB64: [Select]
  1. LINE INPUT #1, LineTxt

LINE INPUT will include commas in a line, without LINE, commas will start a new line.
« Last Edit: March 15, 2020, 01:47:56 pm by bplus »

Offline IronMan

  • Newbie
  • Posts: 38
    • View Profile
Re: Changes to Text File
« Reply #10 on: March 15, 2020, 01:54:35 pm »
Reply Number 2.  Replace text$ with LineTxt.  Remove the 2 idiotic auto- periods my iPad inserted.  Put in place of your PRINT line.  Compile and done.

Thank you SMcNeill. Works perfect!

Offline IronMan

  • Newbie
  • Posts: 38
    • View Profile
Re: Changes to Text File
« Reply #11 on: March 15, 2020, 03:33:38 pm »
Here is what I have so for and it works perfect when printing to screen with the PRINT LineTxt; but I'm needing it to create another text file "test.txt" or whatever that will have the changes made. I tried to do a Close #1 and the Open "test.txt" FOR OUTPUT AS #1 and WRITE #1 LineTxt but that did not seem to work. Will not allow me to open for output until I close the first open statement, and no matter where I seem to put it in my program I get an error.

This is what I have so far:


DIM LineTxt AS STRING
CLS
OPEN "forecast.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
    INPUT #1, LineTxt
    IF LEFT$(LineTxt, 1) = "." THEN
        PRINT CHR$(13); CHR$(13)
        PRINT MID$(LineTxt, 2); " ";
    ELSE
               PRINT LineTxt;   ' would like this output to go to test.txt instead of screen print
    END IF
WEND


Please excuse me for being such a newbie and asking so many questions. I really have studied and tried to figure this out on my own.

All help is appreciated.

Thank you.
Kent

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Changes to Text File
« Reply #12 on: March 15, 2020, 03:55:14 pm »
Open for output with a different #... wait you closed #1 for input so should be OK to write #1

You probably want to keep the input file open and write to output file (a different #) at same time (and different file name too),
read a line, write a line... repeat until EOF then close both.
« Last Edit: March 15, 2020, 04:01:19 pm by bplus »

Offline IronMan

  • Newbie
  • Posts: 38
    • View Profile
Re: Changes to Text File
« Reply #13 on: March 15, 2020, 07:08:12 pm »
This is the code I have so far.


DIM LineTxt AS STRING
CLS
OPEN "forecast.txt" FOR INPUT AS #1
OPEN "test.txt" FOR OUTPUT AS #2
WHILE NOT EOF(1)
    INPUT #1, LineTxt
    IF LEFT$(LineTxt, 1) = "." THEN
        PRINT CHR$(13); CHR$(13)
        PRINT MID$(LineTxt, 2); " ";
    ELSE
        WRITE #2, LineTxt 'write output to new file test.txt
        PRINT LineTxt; 'prints output to screen
    END IF
WEND
CLOSE #1
CLOSE #2


The output to the screen is correct, but when it writes it to the test.txt file, the format is wrong and things are missing from it.

EXAMPLE of test.txt

"afternoon"
"then a slight chance of sprinkles late in the"
"afternoon. Cloudy. Highs in the mid 50s. North winds 5 to 10 mph."
"Chance of rain 20 percent."
"around 5 mph."
"showers. Highs in the upper 60s. Northeast winds around 5 mph."
"in the upper 50s. Light and variable winds."
"then a"
"chance of showers in the afternoon. Mostly cloudy. Highs in the"
"mid 70s. North winds around 5 mph. Chance of rain 40 percent."
"showers. Lows in the upper 50s."
"then a chance of"



Any ideas?

Thank you.
Kent

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Changes to Text File
« Reply #14 on: March 15, 2020, 07:16:51 pm »
I've always used PRINT #2, text$

WRITE #2 is something different

And everything you PRINT to screen do the exact same PRINT #2 to the file

    IF LEFT$(LineTxt, 1) = "." THEN
        PRINT CHR$(13); CHR$(13) ' <<<<<<<< this makes empty line PRINT #2, " "
        PRINT MID$(LineTxt, 2); " "; '<<<              and this                    PRINT #2, MID$(lineTxt, 2); " ";
    ELSE
        WRITE #2, LineTxt 'write output to new file test.txt '<<<<<<< PRINT #2, LineTxT
        PRINT LineTxt; 'prints output to screen

I just tested with forecaste.txt and looks good with PRINT #2 instead of WRITE #2
« Last Edit: March 15, 2020, 07:30:50 pm by bplus »