Python| What in this string is braking new line? - python

I am using RegX to write data in a text file to CSV. I am using the following to read in the data and write it to a CSV file.
with open("C:\\ProgramData\\output\\winprefetch_test_edited.txt", "r", encoding="utf8") as file:
print("defo working")
text_raw = ("%s;" % "{;}".join([line.strip().replace("\t", ",") for line in file.readlines()]))
text = re.sub("#.+\B \d.\d{;}", "", text_raw)
arr_prefetch = [r.split(",") for r in [r for r in text.split("{;}")]]
print("arr_prefetch: ", arr_prefetch)
###############################
# Domain writing to csv
fieldnames = 'Disk Name','Potential Credit Card Number', 'Source'
with open("C:\\ProgramData\\FDA\\output\\processed\\winprefetch.csv", 'w', newline='', encoding="utf8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(fieldnames)
writer.writerows([[diskimage_name, *row] for row in arr_prefetch])
csvfile.close()
This Outputs the following
[![Disk Name Potential Credit Card Number Source
\[windows7_pro.001\] 41693184 MPSIGSTUB.EXE <prefetch><os>Windows Vista or Windows 7</os><filename>MPSIGSTUB.EXE</filename><header_size>240</header_size><atime>20190428T13:34:33Z</atime><runs>1</runs><filenames><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CNTDLL.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CKERNEL32.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CAPISETSCHEMA.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CKERNELBASE.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CLOCALE.NLS</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSOFTWAREDISTRIBUTION\x5CDOWNLOAD\x5CINSTALL\x5CMPSIGSTUB.EXE</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CADVAPI32.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CMSVCRT.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CSECHOST.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CRPCRT4.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CVERSION.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCRYPTBASE.DLL</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CTEMP\x5CMPSIGSTUB.LOG</file></filenames><
"
This is one of the line which has been throwing it off
<prefetch><os>Windows Vista or Windows 7</os><filename>NTOSBOOT</filename><header_size>240</header_size><atime>2019-04-28T21:05:42Z</atime><runs>2</runs><filenames><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5C$MFT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CDRIVERS\x5CCDROM.SYS</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CNTPE.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CNT5.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CNTPH.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-HYPER-V-COMMON-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-HYPER-V-COMMON-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-HYPER-V-GUEST-INTEGRATION-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-HYPER-V-GUEST-INTEGRATION-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-MEDIA-FOUNDATION-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-MEDIA-FOUNDATION-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-MEDIA-FOUNDATION-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-MEDIA-FOUNDATION-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-ANYTIME-UPGRADE-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-ANYTIME-UPGRADE-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-ANYTIME-UPGRADE-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-ANYTIME-UPGRADE-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-ANYTIME-UPGRADE-RESULTS-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-ANYTIME-UPGRADE-RESULTS-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-ANYTIME-UPGRADE-RESULTS-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-ANYTIME-UPGRADE-RESULTS-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BACKUP-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BACKUP-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BACKUP-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BACKUP-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BLB-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BLB-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BLB-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BLB-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-ENTERPRISE-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-ENTERPRISE-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-ENTERPRISE-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-ENTERPRISE-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-HOMEBASIC-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-HOMEBASIC-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-HOMEBASIC-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-HOMEBASIC-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-HOMEPREMIUM-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-HOMEPREMIUM-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-HOMEPREMIUM-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-HOMEPREMIUM-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-PROFESSIONAL-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-PROFESSIONAL-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-PROFESSIONAL-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-PROFESSIONAL-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-STARTER-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-STARTER-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-STARTER-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-STARTER-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-ULTIMATE-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-ULTIMATE-CLIENT-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-ULTIMATE-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BRANDING-ULTIMATE-CLIENT-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BUSINESSSCANNING-FEATURE-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BUSINESSSCANNING-FEATURE-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BUSINESSSCANNING-FEATURE-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-BUSINESSSCANNING-FEATURE-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-DRIVERS-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-DRIVERS-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-FEATURES-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-FEATURES-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-FEATURES-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-FEATURES-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-LANGUAGEPACK-PACKAGE-WRAPPER~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-LANGUAGEPACK-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-LANGUAGEPACK-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-REFRESH-LANGUAGEPACK-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-WIRED-NETWORK-DRIVERS-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-WIRED-NETWORK-DRIVERS-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-WIRED-NETWORK-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIENT-WIRED-NETWORK-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIPSINTHELIBRARY-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIPSINTHELIBRARY-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIPSINTHELIBRARY-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CLIPSINTHELIBRARY-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CODECPACK-BASIC-ENCODER-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CODECPACK-BASIC-ENCODER-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CODECPACK-BASIC-PACKAGE-WRAPPER~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CODECPACK-BASIC-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CODECPACK-BASIC-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CODECPACK-BASIC-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-CODECPACK-BASIC-PACKAGE~31BF3856AD364E35~X86~~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-COMMON-DRIVERS-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7600.16385.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-COMMON-DRIVERS-PACKAGE~31BF3856AD364E35~X86~EN-US~6.1.7601.17514.CAT</file><file>\x5CDEVICE\x5CHARDDISKVOLUME1\x5CWINDOWS\x5CSYSTEM32\x5CCATROOT\x5CF750E6C3-38EE-11D1-85E5-00C04FC295EE\x5CMICROSOFT-WINDOWS-COMMON-DRIVERS-PACKAGE~31BF3856AD364E35~X86~~6.1.7600.16385.CAT</file><file>\x5CDEVICE
Can anyone see what character is throwing it off?

Related

How to add columns in the starting position of a csv file in python?

I have a python program that converts csv files to '^' delimiter files. The output files also has new headers.
cc = input("Enter Code \t")
cn = input("Enter Item Number \t")
input_file = input("Enter input file \t")
act = input("Enter Action (Update- N/Delete -Y) \t")
def file_conversion(input_file, output_file_pattern, chunksize):
with open(input_file,"r+") as fin:
# ignore headers of input files
for i in range(1):
fin.__next__()
reader = csv.reader(fin, delimiter=',')
for i, chunk in enumerate(chunked(reader, chunksize)):
with open(output_file_pattern.format(i), 'w', newline='') as fout:
writer = csv.writer(fout,reader,delimiter='^')
writer.writerow(headers)
writer.writerows(chunk)
print("Successfully converted into", output_file_pattern)
The program works now I want to add act,cc and cn in every line of output delimiter files. The lines of output files should start like the line below
act^cc^cn^restofcsvdelimitierfiles.
This solved my problem
def add_column_in_csv(input_file, output_file, transform_row,transform_row1,transform_row2):
with open(input_file, 'r') as read_obj:
with open(output_file, 'w', newline='') as write_obj:
reader = csv.reader(read_obj,delimiter=',')
writer = csv.writer(write_obj)
for row in reader:
transform_row(row,reader.line_num)
transform_row1(row,reader.line_num)
transform_row2(row,reader.line_num)
writer.writerow(row)
add_column_in_csv(path0,path1, lambda row, line_num: row.insert(0, act),lambda row, line_num: row.insert(1,cc),lambda row, line_num: row.insert(2,cn))

Read, then Write CSV with "Non-ISO extended-ASCII" text Encoding

My csv has strings like:
TîezÑnmidnan
I'm trying to use the following below to set up a reader/writer
import csv
# File that will be written to
csv_output_file = open(file, 'w', encoding='utf-8')
# File that will be read in
csv_file = open(filename, encoding='utf-8', errors='ignore')
# Define reader
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
# Define writer
csv_writer = csv.writer(csv_output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
Then iterating over the information read in
# Iterate over the rows in the csv
for idx, row in enumerate(csv_reader):
csv_writer.writerow(row[0:30])
Problem is in my output file I can't get it to show up with that same string. According to my mac, the csv file type has the encoding "Non-ISO extended-ASCII"
I tried various encodings and some would just remove the special characters while others just wouldn't work.
It's weird because I can hard code that string above into a variable and use it without problems, so I assume it's something to do with how I'm reading in the file. If I breakpoint before it writes it shows up as the following in the debugger.
T�ez�nmidnan
I can't convert the file before running it, so the python code has to handle any conversions itself.
The expected output I want would be for it to remain in the output file looking like
TîezÑnmidnan
Adding a link to a sample csv that shows the issue along with a complete version of my code (with some details removed)
Example file to run with this
import tkinter as tk
from tkinter.filedialog import askopenfilename
import csv
import os
root = tk.Tk()
root.withdraw()
# Ask for file
filename = os.path.abspath(askopenfilename(initialdir="/", title="Select csv file", filetypes=(("CSV Files", "*.csv"),)))
# Set output file name
output_name = filename.rsplit('.')
del output_name[len(output_name) - 1]
output_name = "".join(output_name)
output_name += "_processed.csv"
# Using the file that will be written to
csv_output_file = open(os.path.abspath(output_name), 'w', encoding='utf-8')
# Using the file is be read in
csv_file = open(filename, encoding='utf-8', errors='ignore')
# Define reader with , delimiter
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
# Define writer to put quotes around input values with a comma in them
csv_writer = csv.writer(csv_output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
header_row = []
# Iterate over the rows in the csv
for idx, row in enumerate(csv_reader):
if idx != 0:
csv_writer.writerow(row)
else:
header_row = row
csv_writer.writerow(header_row)
csv_file.flush()
csv_output_file.flush()
csv_file.close()
csv_output_file.close()
Expected results
Header1,Header2
Value1,TîezÑnmidnan
Actual results
Header1,Header2
Value1,Teznmidnan
Edit:
chardetect gave me "utf-8 with confidence 0.99"

Trying to read files named file1,file2,file3 using for loop in Python

I am pretty new to python and trying to run a script to edit csv files. The problem I am facing is that I need to split the csv files into smaller pieces(as they are large files and getting memory errors) and then run another script to edit the files but when im trying to append these two scripts and run the test, the script is reading only the first small file and not reading the rest of the files.
For example: When I split the main csv file, the files are getting split and the names come as big-1.csv,big-2.csv. Then when the script is picking up the files to edit, only big-1.csv is getting edited and rest are not getting edited.
The script is:
import csv
from csv import DictWriter
divisor = 990
outfileno = 1
outfile = None
with open('MOCK_DATA.csv', 'r', newline='') as infile:
infile_iter = csv.reader(infile, delimiter='\t')
header = next(infile_iter)
for index, row in enumerate(infile_iter):
if index % divisor == 0:
if outfile:
outfile.close()
outfilename = 'big-{}.csv'.format(outfileno)
outfile = open(outfilename, 'w', newline='')
outfileno += 1
writer = csv.writer(outfile, delimiter='\t', quoting=csv.QUOTE_NONE)
writer.writerow(header)
writer.writerow(row)
# Don't forget to close the last file
if outfile:
outfile.close()
#export the data
# with correct quoting, and that you are stuck with what you have.
for i in range(1,2):
with open("big-" + str(i) + ".csv") as people_file:
next(people_file)
corrected_people = []
for person_line in people_file:
chomped_person_line = person_line.rstrip()
person_tokens = chomped_person_line.split(",")
# check that each field has the expected type
try:
corrected_person = {
"id": person_tokens[0],
"first_name":person_tokens[1],
"last_name": "".join(person_tokens[2:-3]),
"email":person_tokens[-3],
"gender":person_tokens[-2],
"ip_address":person_tokens[-1]
}
if not corrected_person["ip_address"].startswith(
"") and corrected_person["ip_address"] !="n/a":
raise ValueError
corrected_people.append(corrected_person)
except (IndexError, ValueError):
# print the ignored lines, so manual correction can be performed later.
print("Could not parse line: " + chomped_person_line)
with open("fix-" + str(i) + ".csv", "w") as corrected_people_file:
writer = DictWriter(
corrected_people_file,
fieldnames=[
"id","first_name","last_name","email","gender","ip_address"
],delimiter=',')
writer.writeheader()
writer.writerows(corrected_people)
I think this maybe an issue with reading the smaller files in the for loop. The script is running without any error. Please help.

how do you print to .csv for each ID in Python

import urllib
import re
import csv
player_code = open("Desktop/OHL PYTHON/test2.txt").read()
player_code = player_code.split("\r")
for pc in player_code:
htmlfile = urllib.urlopen( "http://www.eliteprospects.com/iframe_player_stats_small.php?player="+pc+"")
htmltext = htmlfile.read()
regex = '<font color="#000099">(.+?)</font>'
pattern = re.compile(regex)
team = re.findall(pattern,htmltext)
data = pc, team
with open('my_games.csv', 'w') as csvfile:
fieldnames = ['pc', 'team','League', 'Gp', 'G','A','P','Pims']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames,delimiter= ":",
extrasaction ='ignore')
i=0
writer.writeheader()
for pc in player_code:
writer.writerow({'pc':[pc],'team':[team]})
i+=1
This is only returning one line of data over and over. Any direction would be helpful! Thank you.
You should open file before the for loop or use a+ mode. w opens the file and truncate its data each time it is opened.
with open('my_games.csv', 'w') as csvfile:
for pc in player_code:
or
with open('my_games.csv', 'a+') as csvfile
Opening file once would be a better approach.
Since you are looping twice for writing data you have multiple lines. Just remove
i=0
for pc in player_code:
writer.writerow({'pc':[pc],'team':[team]})
i+=1
And instead just have and i think it will work.
writer.writerow({'pc':[pc],'team':[team]})

Python CSV read file and select columns and write to new CSV file

I have a CSV file which has certain columns which I need to extract. One of those columns is a text string from which I need to extract the first and last items. I have a print statement in a for loop which get exactly what I need but cannot figure out how to either get that data into a list or dict. Not sure which is the best to use.
Code so far:
f1 = open ("report.csv","r") # open input file for reading
users_dict = {}
with open('out.csv', 'wb') as f: # output csv file
writer = csv.writer(f)
with open('report.csv','r') as csvfile: # input csv file
reader = csv.DictReader(csvfile, delimiter=',')
for row in reader:
print row['User Name'],row['Address'].split(',')[0],row['Last Login DateTime'],row['Address'].split(',')[7]
users_dict.update(row)
#users_list.append(row['Address'].split(','))
#users_list.append(row['Last Login DateTime'])
#users_list.append(row[5].split(',')[7])
print users_dict
f1.close()
Input from file:
User Name,Display Name,Login Name,Role,Last Login DateTime,Address,Application,AAA,Exchange,Comment
SUPPORT,SUPPORT,SUPPORT,124,2015-05-29 14:32:26,"Test Company,Bond St,London,London,1111 111,GB,test#test.com,IS",,,LSE,
Output on print:
SUPPORT Test Company 2015-05-29 14:32:26 IS
Using this code, I've got the line you need:
import csv
f1 = open ("report.csv","r") # open input file for reading
users_dict = {}
with open('out.csv', 'wb') as f: # output csv file
writer = csv.writer(f)
with open('report.csv','r') as csvfile: # input csv file
reader = csv.DictReader(csvfile, delimiter=',')
for row in reader:
print row['User Name'],row['Address'].split(',')[0],row['Last Login DateTime'],row['Address'].split(',')[7]
users_dict.update(row)
#users_list.append(row['Address'].split(','))
#users_list.append(row['Last Login DateTime'])
#users_list.append(row[5].split(',')[7])
print users_dict
f1.close()
The only changes:
Including the import csv at the top.
Indenting the code just after the with open('out.csv' ......
Does this solve your problem?
With some testing I finally get the line to write the csv file:
for row in reader:
writer.writerow([row['User Name'],row['Address'].split(',')[0],row['Last Login DateTime'],row['Address'].split(',')[7]])

Categories