how do you print to .csv for each ID in Python - 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]})

Related

How to extract one column to another file from a 300GB file

Problem was the huge data number, and I have to do it with my personal laptop with 12GB RAM. I tried a loop with 1M. lines every round, and used csv.writer. But csv.writer wrote like 1M. lines every two hours. So, any other ways worth to try?
lines = 10000000
for i in range(0, 330):
list_str = []
with open(file, 'r') as f:
line_flag = 0
for _ in range(i*lines):
next(f)
for line in f:
line_flag = line_flag + 1
data = json.loads(line)['name']
if data != former_str:
list_str.append(data)
former_str = data
if line_flag == lines:
break
with open(self.path + 'data_range\\names.csv', 'a', newline='') as writeFile:
writer = csv.writer(writeFile, delimiter='\n')
writer.writerow(list_str)
writeFile.close()
another version
def read_large_file(f):
block_size = 200000000
block = []
for line in f:
block.append(line[:-1])
if len(block) == block_size:
yield block
block = []
if block:
yield block
def split_files():
with open(write_file, 'r') as f:
i = 0
for block in read_large_file(f):
print(i)
file_name = write_name + str(i) + '.csv'
with open(file_name, 'w', newline='') as f_:
writer = csv.writer(f_, delimiter='\n')
writer.writerow(block)
i += 1
This was after it read a block and writing ... I wonder how come the rate of data trasmission was keeping about 0.
It should be as simple as this:
import json
import csv
with open(read_file, 'rt') as r, open(write_file, 'wt', newline='') as w:
writer = csv.writer(w)
for line in r:
writer.writerow([json.loads(line)['name']])
I tried the loop inside the file, but I always get me a Error, I guessed we cannot write the data into another file while opening the file?
You totally can write data in one file while reading another. I can't tell you more about your error until you post what it said, though.
There was a bit in your code about former_str which is not covered under "extract one column", so I did not write anything about it.
Would something like this work?
Essentially using a generator to avoid reading the entire file in memory, and writing the data one line at a time.
import jsonlines # pip install jsonlines
from typing import Generator
def gen_lines(file_path: str, col_name: str) -> Generator[str]:
with jsonline.open(file_path) as f:
for obj in f:
yield obj[col_name]
# Here you can also change to writing a jsonline again
with open(output_file, "w") as out:
for item in gen_lines(your_file_path, col_name_to_extract):
out.write(f"{item}\n")

.writerow() csv in Python not writing all data

I'm new to Python and I'm trying to scrape some data and save them in a csv.
I'm trying to loop a csv with a list of URLs, read the data from each URL and write that information in another csv file
The following code is writing roughly half of the data in the cvs but is printing everything fine while it's writing
df_link = pd.read_csv('url_list')
with open('url_list.csv', newline='') as urls, open('output.csv', 'w', newline='') as output:
csv_urls = csv.reader(urls)
csv_output = csv.writer(output)
csv_output.writerow(['details','date'])
for link in df_link.iterrows():
url = link[1]['url']
browser.get(url)
soup = BeautifulSoup(browser.page_source)
csv_file = open('output.csv', 'w')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['details'])
details=[i.text for i in soup.find_all(class_='product-info-content-
block product-info')]
print('details :', details)
dt = date.today()
print('date :', dt)
csv_output.writerow([str(details).strip('[]'), dt])
csv_file.close()
Everything is being printed fine when the code is running, but not all the rows of data are being written in the output csv.
I hope someone can help.
Thank you!
It looks like you are opening output.csv twice, once in the beginning and then in the for loop. Since you are opening with the option w like csv_file = open('output.csv', 'w') it will overwrite the file every loop.
So if you move the below part out of the loop it might work better
csv_file = open('output.csv', 'w')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['details'])

Python| What in this string is braking new line?

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?

How to read a CSV and adapt + write every row to another CSV?

I tried this but it just writes "lagerungskissen kleinkind,44" several times instead of transferring every row.
keyword = []
rank = []
rank = list(map(int, rank))
data = []
with open("keywords.csv", "r") as file:
for line in file:
data = line.strip().replace('"', '').split(",")
keyword = data[0]
rank = data[3]
import csv
with open("mynew.csv", "w", newline="") as f:
thewriter = csv.writer(f)
thewriter.writerow(["Keyword", "Rank"])
for row in keyword:
thewriter.writerow([keyword, rank])
It should look like this
This is writing the same line in your output CSV because the final block is
for row in keyword:
thewriter.writerow([keyword, rank])
Note that the keyword variable doesn't change in the loop, but the row does. You're writing that same [keyword, rank] line len(keyword) times.
I would use the csv package to do the reading and the writing for this. Something like
import csv
input_file = '../keywords.csv'
output_file = '../mynew.csv'
# open the files
fIn = open(input_file, 'r', newline='')
fOut = open(output_file, 'w')
csvIn = csv.reader(fIn, quotechar='"') # check the keyword args in the docs!
csvOut = csv.writer(fOut)
# write a header, then write each row one at a time
csvOut.writerow(['Keyword', 'Rank'])
for row in csvIn:
keyword = row[0]
rank = row[3]
csvOut.writerow([keyword, rank])
# and close the files
fOut.close()
fIn.close()
As as side note, you could write the above using the with context manager (e.g. with open(...) as file:). The answer here shows how to do it with multiple files (in this case fIn and fOut).

Save CSV file using python

I am able to change the data to lowercase and remove all the punctuation but I have trouble saving the corrected data in CSV file.
import csv
import re
import os
input_file=raw_input("Name of the CSV file:")
output_file=raw_input("Output Name:")
reg_test=input_file
result = ''
with open(input_file,'r') as csvfile:
with open(output_file,'w') as csv_out_file:
filereader = csv.reader(csvfile)
filewriter =csv.writer(csv_out_file)
for row in filereader:
row = re.sub('[^A-Za-z0-9]+', '', str(row))
result += row + ','
lower = (result).lower()
csvfile.close()
csv_out_file.close()
You do not have to close the files, this is done automatically after the context of the with statement is over and you have to actually write something after you create the csv.writer, e.g. with writerow:
import csv
import re
input_file = 'in.csv'
output_file = 'out.csv'
with open(input_file, 'r') as csvfile, open(output_file, 'w') as csv_out_file:
filereader = csv.reader(csvfile)
filewriter = csv.writer(csv_out_file)
for row in filereader:
new_row = re.sub('[^A-Za-z0-9]+', '', str(row)) # manipulate the row
filewriter.writerow([new_row.lower()]) # write the new row to the out file
# the files are closed automatically after the context of the with statement is over
This saves the manipulated content of the first csv file to the second.

Categories