1

I have two txt files:

Dummy-dates.txt contains:
0/0/0000|
1/0/0000|
2/0/0000|
etc... up to 31.

single-dates.txt contains:
2/0/0000|Johns Bday
6/0/0000|Some other cool date
etc.. can have random number of lines within 1-31 in the start

I need to create a new txt file containing:
0/0/0000|
1/0/0000|
2/0/0000|Johns Bday
3/0/0000|
4/0/0000|
5/0/0000|
6/0/0000|Some other cool date
7/0/0000|
etc.... up to 31

I can not figure this out - I've tried with a nested for loop. Can anyone help me out?

Jacob
  • 29
  • 4
  • You should post your code. It doesn't take a nested loop, but it takes two loops. Read the update file (single-dates.txt) into a dictionary, where the key is the date, and the value is the string. Then, open the input file and your new output file. When the date is found in your list, write out the value. When it's not, just copy the input line. – Tim Roberts Mar 11 '21 at 01:25

1 Answers1

0

This actually will help you, but once again, this is hard line, only for your specific file

def get_line(file_name1,file_name2, line_start1,line_end1, line_start2, line_end2, file_name3):
    f1 = open(file_name1, 'r+')
    f2= open(file_name2, 'r+')
    f3=open(file_name3,'w+')
    line1=f1.readlines(line_start1)    
    line2=f2.readlines(line_start2)
    line1=line1[:line_end1]
    line2=line2[:line_end2]
    for i in range(len(line2)):
      line3="".join(line2[i].split("|")[0]).replace("/","")
      for k in range(len(line1)):
        line4="".join(line1[k].split("|")[0]).replace("/","")
        if(line3==(line4)):
          line1[k]=line2[i]
    f3.writelines(line1)
    f1.close()
    f2.close()
    f3.close()

get_line('temp/Dummy-dates-1.txt', 'temp/Separate-dates-1.txt', 0, 31, 0, 31, 'temp/Combined-1.txt')
JohntheTyro
  • 136
  • 4
  • The biggest problem of last code I gave you it the re.split(), this actually split the String piece by piece, it didn't really help us to calculate the sum of date, so that the index can't be targeted. But this one, I tried to use the original split(), hoping this will work. – JohntheTyro Mar 11 '21 at 21:31
  • note sure that, if(sum<=line_end1), is actually a check to verify the index will not exceed the max index you have in file1. The best solution will be check if there a index in file1 which the sum of the date will be as same as file2, but in my code, I didn't write. If you want to have a perfect check, you can write it yourself, that can also guarantee that if the dates are messed in file1, file 2 data will not overwrite the file1 in that specific date. – JohntheTyro Mar 11 '21 at 21:40
  • Thank you for taking your time with this. but repl.it still call out an error. In line 17 I get an error: sum += int(k) ValueError: invalid literal for int() with base 10: '' – Jacob Mar 11 '21 at 22:09
  • can you try to print(k) before sum+=int(k),to see what is other than digit as k? if it is not digit, let me know – JohntheTyro Mar 11 '21 at 22:15
  • k prints out 01 01 2021 06 01 – Jacob Mar 11 '21 at 22:19
  • so if you run it on cmd, even it will give u a error calling, but before that it will still print stuff, check what element in that array that can't make int() works – JohntheTyro Mar 11 '21 at 22:20
  • ok so now it completes an make the new file - but i looks completely like Dummy-dates.txt without the last 31/1/2021| line – Jacob Mar 11 '21 at 22:24
  • I know this issue, give me a sec – JohntheTyro Mar 11 '21 at 22:26
  • It does not add the rest (right side of |) – Jacob Mar 11 '21 at 22:26
  • yea, where of your date begins with? dummy.txt – JohntheTyro Mar 11 '21 at 22:46
  • its 31 lines: 0/1/2021| 1/1/2021| 2/1/2021| 3/1/2021| 4/1/2021| 5/1/2021| 6/1/2021| 7/1/2021| 8/1/2021| 9/1/2021| 10/1/2021| 11/1/2021| 12/1/2021| 13/1/2021| 14/1/2021| 15/1/2021| 16/1/2021| 17/1/2021| 18/1/2021| 19/1/2021| 20/1/2021| 21/1/2021| 22/1/2021| 23/1/2021| 24/1/2021| 25/1/2021| 26/1/2021| 27/1/2021| 28/1/2021| 29/1/2021| 30/1/2021| 31/1/2021| – Jacob Mar 11 '21 at 22:49
  • I can link you a repl.it if you want – Jacob Mar 11 '21 at 22:51
  • a better link: https://repl.it/@Felledk/calendar-txt-files – Jacob Mar 11 '21 at 23:06
  • I modified how you created the file too, you didn't add '0' in front of each month or day which smaller than 10, however, your seperateion file did have 0 before that, like 01/01/ – JohntheTyro Mar 11 '21 at 23:45
  • PERFECT! Thank you so much!!! . any idea why the last row is not applied? – Jacob Mar 11 '21 at 23:54
  • I used to read it with a int() wrapped around so it understood the single digit. Super cool to have it working finally. Now I can complete my calendar project :) – Jacob Mar 11 '21 at 23:56
  • aight, no problem, feel free to ask if you have any further questions. – JohntheTyro Mar 12 '21 at 00:02