Welcome, Guest

Author Topic: python challenge  (Read 3538 times)

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
python challenge
« on: January 16, 2013, 12:40:00 AM »
write the code needed to take the following file as a .txt input, then add discovered_by = sandboxia after the other discovered_by statements IF discovered_by = western exists., then save the file.


Code: [Select]
#Uppland, contains Stockholm, Uppsala & Nyköping.

add_core = SWE
owner = SWE
controller = SWE
culture = swedish
religion = catholic
hre = no
base_tax = 5
trade_goods = grain
manpower = 3
fort1 = yes
capital = "Stockholm"
citysize = 5500
temple = yes #"Domkyrkan i Uppsala"
workshop = yes #"Stockholms borgarskap"
marketplace = yes
discovered_by = eastern
discovered_by = western
discovered_by = muslim
discovered_by = ottoman

1436.4.27  = { revolt = { type = nationalist_rebels size = 2 } controller = REB } # Engelbrekt and Karl marsh on Stockholm
1448.6.20  = {  revolt = {} controller = SWE } # Karl VIII is elected king of Sweden
1450.1.1   = { citysize = 6500 }
# 1457.2.24 = { owner = DAN controller = DAN } # Karl VIII exiled and Kristian I is elected king
# 1464.9.1 = { owner = SWE controller = SWE } # Kristian is driven out of Sweden and Karl is reinstated
1477.1.1   = { university = yes } # Uppsala University
1501.8.1   = { controller = DAN } # Danish loyalists at Swedish DoW and breaking of vassalage
1502.5.9   = { controller = SWE } # Retaken by Sweden
# 1520.9.2 = { owner = DAN controller = DAN } # Christian II conquers Stockholm
1523.6.7   = { regimental_camp = yes decision = establish_recruiting_centre } # Kgl.DrabantkÃ¥ren/Svea Livgarde
# 1523.6.21 = { owner = SWE controller = SWE } # Gustav Eriksson Wasa enters on midsummer day
1527.6.1   = { religion = protestant}
1537.1.1   = { fort2 = yes } # Gripsholms Castle
1570.1.1   = { citysize = 10000 }
1598.8.12  = { controller = POL } # Sigismund tries to reconquer his crown
1598.12.15 = { controller = SWE } # Duke Karl get it back
1608.1.1   = { constable = yes } # The men around Axel Oxenstierna
1614.1.1   = { courthouse = yes } # Svea Hovrätt
1617.1.1   = { cot = yes } # Stockholm commanded the Baltic Sea trade
1617.1.1   = { base_tax = 6 } # Stockholm become Baltic Metropol
1621.1.1   = { fort3 = yes } # Key forts defending roads to Stockholm
1630.1.1   = { citysize = 20000 }
1634.1.1   = { shipyard = yes } # Skeppsholmen issued as naval base by GIIA
1650.1.1   = { citysize = 38000 }
1654.1.1   = { tax_assessor = yes } # Result of "The Great Reduction"
1680.1.1   = { fort4 = yes } # Added forts giving coastal defense of region
1690.1.1   = { citysize = 59000 }
1704.1.1   = { war_college = yes } # estimated date
#1710.1.1   = { customs_house = yes } # Baron v. Görtz new institution
1730.1.1   = { citysize = 60000 }
1730.1.1   = { fort5 = yes } # estimated date
1750.1.1   = { fort6 = yes } # estimated date
1770.1.1   = { citysize = 70000 }
1810.1.1   = { citysize = 65000 }

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: python challenge
« Reply #1 on: January 16, 2013, 07:30:38 AM »
Done.

Fixed. This should work.

I only tested it for one file though, but it should work.

Note: You have to edit the directory.
« Last Edit: January 16, 2013, 08:16:03 AM by atomic7732 »

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: python challenge
« Reply #2 on: January 16, 2013, 01:49:24 PM »
omg so epic thx

edit: protip_to_self: escape slashes

edit:edit: there seems to be a lot of whitespace added to the front of the text files, but that's not an issue anyways, i just wanted to note it.
« Last Edit: January 16, 2013, 02:07:37 PM by vh »

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: python challenge
« Reply #3 on: January 16, 2013, 03:16:26 PM »
Really? I noticed it was doing that before but I thought I fixed it.

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: python challenge
« Reply #4 on: January 16, 2013, 03:18:13 PM »
yes its still like that according to notepad it's not actually whitespace but a bunch of NULL (or maybe they're the same thing idk) but what caused it in the first place that you fixed?

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: python challenge
« Reply #5 on: January 16, 2013, 03:20:50 PM »
it was putting it in the wrong spot likely due to me putting truncate() wrong I don't know actually so afsdgfdsj i just thought it went away

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: python challenge
« Reply #6 on: January 16, 2013, 03:22:24 PM »
well as long as the game runs fine i don't really care

Darvince

  • *****
  • Posts: 1842
  • 差不多
Re: python challenge
« Reply #7 on: January 16, 2013, 04:22:02 PM »
TIL eu3 is sovktech

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: python challenge
« Reply #8 on: January 16, 2013, 10:39:20 PM »
omg all the nullchars that your script adds apparenly breaks the game. breaks it!

\x00 is the null character thingy so maybe it is possible to take the entire text file and remove all '\x00'?

atomic7732

  • Global Moderator
  • *****
  • Posts: 3849
  • caught in the river turning blue
    • Paladin of Storms
Re: python challenge
« Reply #9 on: January 17, 2013, 06:29:00 AM »
i suppose but i have no idea why it adds them in the first place

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: python challenge
« Reply #10 on: January 17, 2013, 01:15:24 PM »
me neither here is a tested version that works:

Code: [Select]
import os
import re

location = 'C:\\Users\\ricson3\\Desktop\\Narmod\\history\\jerper\\'
list = []
appendything = 'discovered_by = sandboxia\n'

os.chdir(location)
for files in os.listdir('.'):
if files.endswith('.txt'):
list.append(files)

print list

for file in list:
f = open(file, 'r+')
search = f.read()
index = search.find('discovered_by = western')
f.seek(0)
p1 = f.read(index+len('discovered_by = western')+1)
p2 = f.read()
f.truncate(0)
f.write(p1)
f.write(appendything)
f.write(p2)
f.close
f = open(file, 'r+')
stringer = f.read()
new_str = re.sub('\x00', '', stringer)
f.seek(0)
f.write(new_str)
f.truncate()
f.close()

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: python challenge
« Reply #11 on: January 17, 2013, 02:03:02 PM »
AH no, the nullspaces weren't the problem i dont' think but the python program changes

discovered_by = chinese
1542.1.1 = { discovered_by = POR }

to

discovered_by = chinesediscovered_by = sandboxia
1542.1.1 = { discovered_by = POR }

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: python challenge
« Reply #12 on: January 17, 2013, 02:04:25 PM »
and it changes

Code: [Select]
#905 - Iowa

culture = dakota
religion = shamanism
capital = "Iowa"
trade_goods = unknown
hre = no
base_tax = 2
manpower = 1
native_size = 25
native_ferocity = 2
native_hostileness = 9

1684.1.1  = { discovered_by = FRA } # Nicolas Perrot
1727.1.1  = { owner = FRA
controller = FRA
citysize = 522
culture = cosmopolitan_french
religion = catholic
trade_goods = grain
    } # Several smaller settlements
1752.1.1  = { add_core = FRA }
1763.2.10 = { owner = GBR
controller = GBR
remove_core = FRA
culture = english
religion = protestant
   } # Treaty of Paris - ceded to Britain, France gave up its claim
1783.9.3 = { owner = USA
controller = USA
add_core = USA
culture = american
religion = protestant
   }
1800.1.1 = { citysize = 2870 }


to

Code: [Select]
#905 - Iowa

culture = discovered_by = sandboxia
dakota
religion = shamanism
capital = "Iowa"
trade_goods = unknown
hre = no
base_tax = 2
manpower = 1
native_size = 25
native_ferocity = 2
native_hostileness = 9

1684.1.1  = { discovered_by = FRA } # Nicolas Perrot
1727.1.1  = { owner = FRA
controller = FRA
citysize = 522
culture = cosmopolitan_french
religion = catholic
trade_goods = grain
    } # Several smaller settlements
1752.1.1  = { add_core = FRA }
1763.2.10 = { owner = GBR
controller = GBR
remove_core = FRA
culture = english
religion = protestant
   } # Treaty of Paris - ceded to Britain, France gave up its claim
1783.9.3 = { owner = USA
controller = USA
add_core = USA
culture = american
religion = protestant
   }
1800.1.1 = { citysize = 2870 }

kalassak wth were you smoking

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: python challenge
« Reply #13 on: January 17, 2013, 02:18:37 PM »
ohhhh

the problem was that you didn't prepare for the eventuality of a province NOT discovered by western which is remedied by changing the bottom portion of the script to

Code: [Select]
for file in list:
f = open(file, 'r+')
search = f.read()
index = search.find('discovered_by = western')
if index == -1:
pass
else:
f.seek(0)
p1 = f.read(index+len('discovered_by = western')+1)
p2 = f.read()
f.truncate(0)
f.write(p1)
f.write(appendything)
f.write(p2)
f.close
f = open(file, 'r+')
stringer = f.read()
new_str = re.sub('\x00', '', stringer)
f.seek(0)
f.write(new_str)
f.truncate()
f.close()