def save_csv(result, path): """Save csv at the given path.""" base_path = os.path.dirname(path) if not os.path.exists(base_path): os.makedirs(base_path) with open(path, 'w') as f: csv.dump(result, f)
def start(): address_book = {} # 공백 딕셔너리를 생성한다. try: with open("./1.csv", "r", encoding='utf-8') as f: #파일 불러오기 address_book = csv.reader(f) #csv파일을 address에 저장 for line in address_book: #address를 line print(line) f.close() print("파일을 불러왔습니다", address_book) except FileNotFoundError as e: print('파일이 존재하지 않습니다..', e) f = open('./1.csv', 'w', encoding='utf-8') # 파일 불러오기 while True: user = display_menu() if user == 1: # 추가 f = open('./1.csv', 'w', encoding='utf-8') #파일 불러오기 wr = csv.writer(f) #파일쓰기 변수 선언 number, name, phone, date, time = get_contact( ) #contact메소드 통한 데이터입력 info = [number, name, phone, date, time] #데이터 배열에 저장 print('info', info) #배열을 화면에 출력 wr.writerow(info) #배열을 파일 쓰기 elif user == 2: # 삭제 number = input("삭제할 번호 입력 : ") for row in address_book: if row[0] != number: wr.writerow(row) f.close() print(f'{number} 삭제 되었습니다') elif user == 3: # 검색 select = input("검색할 번호 입력 : ") if address_book(select) is None: print("저장되지 않은 번호입니다.") else: f = address_book(select) print(select, "의 번호 :", f) elif user == 4: # 출력 f = open('./1.csv', 'r', encoding='utf-8') rdr = csv.reader(f) for line in rdr: print(line) f.close() else: # 파일 저장 with open("./1.csv", "wb") as f: csv.dump(address_book, f) f.close() print("파일이 저장되었습니다!") break
def write_wertpapier_csv(self, wertpapier): self.wertpapier = wertpapier csvfile = open( os.path.join(self.data_directory, str(self.wertpapier.kürzel) + '.csv'), 'w') csvdata = self.wertpapier # ===== Zeile noch falch ==================== csv.dump(csvdata, csvfile) csvfile.close()
class AirbnbScraper: def __init__(self, debug=True): self.cookies = {} self.debug = debug def fixDetail(mainResults, indexList): finalResults = mainResults[:] baseURL = 'https://www.airbnb.com/rooms/' for i in indexList: print ('fixing index %s' % str(i)) listingID = str(finalResults[i]['ListingID']) currentURL = ''.join([baseURL, listingID]) # Collect Data newListing = dict(finalResults[i].items() + currentURL.items()) finalResults[i] = newListing return finalResults def getTree(url): try: tree = urlopen(open(url).get_data()) return tree except: print ('Was not able to fetch data from %s' % url) return '' def collectDetail(treeObject, ListingID): Results = {'AboutListing': 'Not Found', 'HostName': 'Not Found', 'RespRate': 'Not Found', 'RespTime': 'Not Found', 'MemberDate': 'Not Found', 'R_acc': 'Not Found', 'R_comm': 'Not Found', 'R_clean': 'Not Found', 'R_loc': 'Not Found', 'R_CI': 'Not Found', 'R_val': 'Not Found', 'P_ExtraPeople': 'Not Found', 'P_Cleaning': 'Not Found', 'P_Deposit': 'Not Found', 'P_Weekly': 'Not Found', 'P_Monthly': 'Not Found', 'Cancellation': 'Not Found', 'A_Kitchen': 0, 'A_Internet': 0, 'A_TV': 0, 'A_Essentials': 0, 'A_Shampoo': 0, 'A_Heat': 0, 'A_AC': 0, 'A_Washer': 0, 'A_Dryer': 0, 'A_Parking': 0, 'A_Internet': 0, 'A_CableTV': 0, 'A_Breakfast': 0, 'A_Pets': 0, 'A_FamilyFriendly': 0, 'A_Events': 0, 'A_Smoking': 0, 'A_Wheelchair': 0, 'A_Elevator': 0, 'A_Fireplace': 0, 'A_Intercom': 0, 'A_Doorman': 0, 'A_Pool': 0, 'A_HotTub': 0, 'A_Gym': 0, 'A_SmokeDetector': 0, 'A_CarbonMonoxDetector': 0, 'A_FirstAidKit': 0, 'A_SafetyCard': 0, 'A_FireExt': 0, 'S_PropType': 'Not Found', 'S_Accomodates': 'Not Found', 'S_Bedrooms': 'Not Found', 'S_Bathrooms': 'Not Found', 'S_NumBeds': 'Not Found', 'S_BedType': 'Not Found', 'S_CheckIn': 'Not Found', 'S_Checkout': 'Not Found' } offset = 0 listings = [] crawled_listings = set() if len(Results['listings']) == 0: break else: new_listings = [{k: listing['listing'].get(k, None) for k in Results} for listing in Results['listings'] if listing['listing']['id'] not in crawled_listings] for new_listing in new_listings: new_listing['calendar'] = (new_listing['id']) listings.extend([listing for listing in new_listings if listing['id'] not in crawled_listings]) crawled_listings.update(listing['id'] for listing in new_listings) with codecs.open('listings.json', 'w', encoding='utf-8') as f: csv.dump(listings, f)
def exportToCsv(self) : csv.dump(dbs, 'dbs.json')